s5p_mfc_opr.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. /*
  2. * drivers/media/video/samsung/mfc5/s5p_mfc_opr.c
  3. *
  4. * Samsung MFC (Multi Function Codec - FIMV) driver
  5. * This file contains hw related functions.
  6. *
  7. * Kamil Debski, Copyright (c) 2011 Samsung Electronics
  8. * http://www.samsung.com/
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include "regs-mfc.h"
  15. #include "s5p_mfc_cmd.h"
  16. #include "s5p_mfc_common.h"
  17. #include "s5p_mfc_ctrl.h"
  18. #include "s5p_mfc_debug.h"
  19. #include "s5p_mfc_intr.h"
  20. #include "s5p_mfc_opr.h"
  21. #include "s5p_mfc_pm.h"
  22. #include "s5p_mfc_shm.h"
  23. #include <asm/cacheflush.h>
  24. #include <linux/delay.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/err.h>
  27. #include <linux/firmware.h>
  28. #include <linux/io.h>
  29. #include <linux/jiffies.h>
  30. #include <linux/mm.h>
  31. #include <linux/sched.h>
  32. #define OFFSETA(x) (((x) - dev->bank1) >> MFC_OFFSET_SHIFT)
  33. #define OFFSETB(x) (((x) - dev->bank2) >> MFC_OFFSET_SHIFT)
  34. /* Allocate temporary buffers for decoding */
  35. int s5p_mfc_alloc_dec_temp_buffers(struct s5p_mfc_ctx *ctx)
  36. {
  37. void *desc_virt;
  38. struct s5p_mfc_dev *dev = ctx->dev;
  39. ctx->desc_buf = vb2_dma_contig_memops.alloc(
  40. dev->alloc_ctx[MFC_BANK1_ALLOC_CTX], DESC_BUF_SIZE);
  41. if (IS_ERR_VALUE((int)ctx->desc_buf)) {
  42. ctx->desc_buf = 0;
  43. mfc_err("Allocating DESC buffer failed\n");
  44. return -ENOMEM;
  45. }
  46. ctx->desc_phys = s5p_mfc_mem_cookie(
  47. dev->alloc_ctx[MFC_BANK1_ALLOC_CTX], ctx->desc_buf);
  48. BUG_ON(ctx->desc_phys & ((1 << MFC_BANK1_ALIGN_ORDER) - 1));
  49. desc_virt = vb2_dma_contig_memops.vaddr(ctx->desc_buf);
  50. if (desc_virt == NULL) {
  51. vb2_dma_contig_memops.put(ctx->desc_buf);
  52. ctx->desc_phys = 0;
  53. ctx->desc_buf = 0;
  54. mfc_err("Remapping DESC buffer failed\n");
  55. return -ENOMEM;
  56. }
  57. memset(desc_virt, 0, DESC_BUF_SIZE);
  58. wmb();
  59. return 0;
  60. }
  61. /* Release temporary buffers for decoding */
  62. void s5p_mfc_release_dec_desc_buffer(struct s5p_mfc_ctx *ctx)
  63. {
  64. if (ctx->desc_phys) {
  65. vb2_dma_contig_memops.put(ctx->desc_buf);
  66. ctx->desc_phys = 0;
  67. ctx->desc_buf = 0;
  68. }
  69. }
  70. /* Allocate codec buffers */
  71. int s5p_mfc_alloc_codec_buffers(struct s5p_mfc_ctx *ctx)
  72. {
  73. struct s5p_mfc_dev *dev = ctx->dev;
  74. unsigned int enc_ref_y_size = 0;
  75. unsigned int enc_ref_c_size = 0;
  76. unsigned int guard_width, guard_height;
  77. if (ctx->type == MFCINST_DECODER) {
  78. mfc_debug(2, "Luma size:%d Chroma size:%d MV size:%d\n",
  79. ctx->luma_size, ctx->chroma_size, ctx->mv_size);
  80. mfc_debug(2, "Totals bufs: %d\n", ctx->total_dpb_count);
  81. } else if (ctx->type == MFCINST_ENCODER) {
  82. enc_ref_y_size = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN)
  83. * ALIGN(ctx->img_height, S5P_FIMV_NV12MT_VALIGN);
  84. enc_ref_y_size = ALIGN(enc_ref_y_size, S5P_FIMV_NV12MT_SALIGN);
  85. if (ctx->codec_mode == S5P_FIMV_CODEC_H264_ENC) {
  86. enc_ref_c_size = ALIGN(ctx->img_width,
  87. S5P_FIMV_NV12MT_HALIGN)
  88. * ALIGN(ctx->img_height >> 1,
  89. S5P_FIMV_NV12MT_VALIGN);
  90. enc_ref_c_size = ALIGN(enc_ref_c_size,
  91. S5P_FIMV_NV12MT_SALIGN);
  92. } else {
  93. guard_width = ALIGN(ctx->img_width + 16,
  94. S5P_FIMV_NV12MT_HALIGN);
  95. guard_height = ALIGN((ctx->img_height >> 1) + 4,
  96. S5P_FIMV_NV12MT_VALIGN);
  97. enc_ref_c_size = ALIGN(guard_width * guard_height,
  98. S5P_FIMV_NV12MT_SALIGN);
  99. }
  100. mfc_debug(2, "recon luma size: %d chroma size: %d\n",
  101. enc_ref_y_size, enc_ref_c_size);
  102. } else {
  103. return -EINVAL;
  104. }
  105. /* Codecs have different memory requirements */
  106. switch (ctx->codec_mode) {
  107. case S5P_FIMV_CODEC_H264_DEC:
  108. ctx->bank1_size =
  109. ALIGN(S5P_FIMV_DEC_NB_IP_SIZE +
  110. S5P_FIMV_DEC_VERT_NB_MV_SIZE,
  111. S5P_FIMV_DEC_BUF_ALIGN);
  112. ctx->bank2_size = ctx->total_dpb_count * ctx->mv_size;
  113. break;
  114. case S5P_FIMV_CODEC_MPEG4_DEC:
  115. ctx->bank1_size =
  116. ALIGN(S5P_FIMV_DEC_NB_DCAC_SIZE +
  117. S5P_FIMV_DEC_UPNB_MV_SIZE +
  118. S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE +
  119. S5P_FIMV_DEC_STX_PARSER_SIZE +
  120. S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE,
  121. S5P_FIMV_DEC_BUF_ALIGN);
  122. ctx->bank2_size = 0;
  123. break;
  124. case S5P_FIMV_CODEC_VC1RCV_DEC:
  125. case S5P_FIMV_CODEC_VC1_DEC:
  126. ctx->bank1_size =
  127. ALIGN(S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE +
  128. S5P_FIMV_DEC_UPNB_MV_SIZE +
  129. S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE +
  130. S5P_FIMV_DEC_NB_DCAC_SIZE +
  131. 3 * S5P_FIMV_DEC_VC1_BITPLANE_SIZE,
  132. S5P_FIMV_DEC_BUF_ALIGN);
  133. ctx->bank2_size = 0;
  134. break;
  135. case S5P_FIMV_CODEC_MPEG2_DEC:
  136. ctx->bank1_size = 0;
  137. ctx->bank2_size = 0;
  138. break;
  139. case S5P_FIMV_CODEC_H263_DEC:
  140. ctx->bank1_size =
  141. ALIGN(S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE +
  142. S5P_FIMV_DEC_UPNB_MV_SIZE +
  143. S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE +
  144. S5P_FIMV_DEC_NB_DCAC_SIZE,
  145. S5P_FIMV_DEC_BUF_ALIGN);
  146. ctx->bank2_size = 0;
  147. break;
  148. case S5P_FIMV_CODEC_H264_ENC:
  149. ctx->bank1_size = (enc_ref_y_size * 2) +
  150. S5P_FIMV_ENC_UPMV_SIZE +
  151. S5P_FIMV_ENC_COLFLG_SIZE +
  152. S5P_FIMV_ENC_INTRAMD_SIZE +
  153. S5P_FIMV_ENC_NBORINFO_SIZE;
  154. ctx->bank2_size = (enc_ref_y_size * 2) +
  155. (enc_ref_c_size * 4) +
  156. S5P_FIMV_ENC_INTRAPRED_SIZE;
  157. break;
  158. case S5P_FIMV_CODEC_MPEG4_ENC:
  159. ctx->bank1_size = (enc_ref_y_size * 2) +
  160. S5P_FIMV_ENC_UPMV_SIZE +
  161. S5P_FIMV_ENC_COLFLG_SIZE +
  162. S5P_FIMV_ENC_ACDCCOEF_SIZE;
  163. ctx->bank2_size = (enc_ref_y_size * 2) +
  164. (enc_ref_c_size * 4);
  165. break;
  166. case S5P_FIMV_CODEC_H263_ENC:
  167. ctx->bank1_size = (enc_ref_y_size * 2) +
  168. S5P_FIMV_ENC_UPMV_SIZE +
  169. S5P_FIMV_ENC_ACDCCOEF_SIZE;
  170. ctx->bank2_size = (enc_ref_y_size * 2) +
  171. (enc_ref_c_size * 4);
  172. break;
  173. default:
  174. break;
  175. }
  176. /* Allocate only if memory from bank 1 is necessary */
  177. if (ctx->bank1_size > 0) {
  178. ctx->bank1_buf = vb2_dma_contig_memops.alloc(
  179. dev->alloc_ctx[MFC_BANK1_ALLOC_CTX], ctx->bank1_size);
  180. if (IS_ERR(ctx->bank1_buf)) {
  181. ctx->bank1_buf = 0;
  182. printk(KERN_ERR
  183. "Buf alloc for decoding failed (port A)\n");
  184. return -ENOMEM;
  185. }
  186. ctx->bank1_phys = s5p_mfc_mem_cookie(
  187. dev->alloc_ctx[MFC_BANK1_ALLOC_CTX], ctx->bank1_buf);
  188. BUG_ON(ctx->bank1_phys & ((1 << MFC_BANK1_ALIGN_ORDER) - 1));
  189. }
  190. /* Allocate only if memory from bank 2 is necessary */
  191. if (ctx->bank2_size > 0) {
  192. ctx->bank2_buf = vb2_dma_contig_memops.alloc(
  193. dev->alloc_ctx[MFC_BANK2_ALLOC_CTX], ctx->bank2_size);
  194. if (IS_ERR(ctx->bank2_buf)) {
  195. ctx->bank2_buf = 0;
  196. mfc_err("Buf alloc for decoding failed (port B)\n");
  197. return -ENOMEM;
  198. }
  199. ctx->bank2_phys = s5p_mfc_mem_cookie(
  200. dev->alloc_ctx[MFC_BANK2_ALLOC_CTX], ctx->bank2_buf);
  201. BUG_ON(ctx->bank2_phys & ((1 << MFC_BANK2_ALIGN_ORDER) - 1));
  202. }
  203. return 0;
  204. }
  205. /* Release buffers allocated for codec */
  206. void s5p_mfc_release_codec_buffers(struct s5p_mfc_ctx *ctx)
  207. {
  208. if (ctx->bank1_buf) {
  209. vb2_dma_contig_memops.put(ctx->bank1_buf);
  210. ctx->bank1_buf = 0;
  211. ctx->bank1_phys = 0;
  212. ctx->bank1_size = 0;
  213. }
  214. if (ctx->bank2_buf) {
  215. vb2_dma_contig_memops.put(ctx->bank2_buf);
  216. ctx->bank2_buf = 0;
  217. ctx->bank2_phys = 0;
  218. ctx->bank2_size = 0;
  219. }
  220. }
  221. /* Allocate memory for instance data buffer */
  222. int s5p_mfc_alloc_instance_buffer(struct s5p_mfc_ctx *ctx)
  223. {
  224. void *context_virt;
  225. struct s5p_mfc_dev *dev = ctx->dev;
  226. if (ctx->codec_mode == S5P_FIMV_CODEC_H264_DEC ||
  227. ctx->codec_mode == S5P_FIMV_CODEC_H264_ENC)
  228. ctx->ctx_size = MFC_H264_CTX_BUF_SIZE;
  229. else
  230. ctx->ctx_size = MFC_CTX_BUF_SIZE;
  231. ctx->ctx_buf = vb2_dma_contig_memops.alloc(
  232. dev->alloc_ctx[MFC_BANK1_ALLOC_CTX], ctx->ctx_size);
  233. if (IS_ERR(ctx->ctx_buf)) {
  234. mfc_err("Allocating context buffer failed\n");
  235. ctx->ctx_phys = 0;
  236. ctx->ctx_buf = 0;
  237. return -ENOMEM;
  238. }
  239. ctx->ctx_phys = s5p_mfc_mem_cookie(
  240. dev->alloc_ctx[MFC_BANK1_ALLOC_CTX], ctx->ctx_buf);
  241. BUG_ON(ctx->ctx_phys & ((1 << MFC_BANK1_ALIGN_ORDER) - 1));
  242. ctx->ctx_ofs = OFFSETA(ctx->ctx_phys);
  243. context_virt = vb2_dma_contig_memops.vaddr(ctx->ctx_buf);
  244. if (context_virt == NULL) {
  245. mfc_err("Remapping instance buffer failed\n");
  246. vb2_dma_contig_memops.put(ctx->ctx_buf);
  247. ctx->ctx_phys = 0;
  248. ctx->ctx_buf = 0;
  249. return -ENOMEM;
  250. }
  251. /* Zero content of the allocated memory */
  252. memset(context_virt, 0, ctx->ctx_size);
  253. wmb();
  254. if (s5p_mfc_init_shm(ctx) < 0) {
  255. vb2_dma_contig_memops.put(ctx->ctx_buf);
  256. ctx->ctx_phys = 0;
  257. ctx->ctx_buf = 0;
  258. return -ENOMEM;
  259. }
  260. return 0;
  261. }
  262. /* Release instance buffer */
  263. void s5p_mfc_release_instance_buffer(struct s5p_mfc_ctx *ctx)
  264. {
  265. if (ctx->ctx_buf) {
  266. vb2_dma_contig_memops.put(ctx->ctx_buf);
  267. ctx->ctx_phys = 0;
  268. ctx->ctx_buf = 0;
  269. }
  270. if (ctx->shm_alloc) {
  271. vb2_dma_contig_memops.put(ctx->shm_alloc);
  272. ctx->shm_alloc = 0;
  273. ctx->shm = 0;
  274. }
  275. }
  276. /* Set registers for decoding temporary buffers */
  277. void s5p_mfc_set_dec_desc_buffer(struct s5p_mfc_ctx *ctx)
  278. {
  279. struct s5p_mfc_dev *dev = ctx->dev;
  280. mfc_write(dev, OFFSETA(ctx->desc_phys), S5P_FIMV_SI_CH0_DESC_ADR);
  281. mfc_write(dev, DESC_BUF_SIZE, S5P_FIMV_SI_CH0_DESC_SIZE);
  282. }
  283. /* Set registers for shared buffer */
  284. void s5p_mfc_set_shared_buffer(struct s5p_mfc_ctx *ctx)
  285. {
  286. struct s5p_mfc_dev *dev = ctx->dev;
  287. mfc_write(dev, ctx->shm_ofs, S5P_FIMV_SI_CH0_HOST_WR_ADR);
  288. }
  289. /* Set registers for decoding stream buffer */
  290. int s5p_mfc_set_dec_stream_buffer(struct s5p_mfc_ctx *ctx, int buf_addr,
  291. unsigned int start_num_byte, unsigned int buf_size)
  292. {
  293. struct s5p_mfc_dev *dev = ctx->dev;
  294. mfc_write(dev, OFFSETA(buf_addr), S5P_FIMV_SI_CH0_SB_ST_ADR);
  295. mfc_write(dev, ctx->dec_src_buf_size, S5P_FIMV_SI_CH0_CPB_SIZE);
  296. mfc_write(dev, buf_size, S5P_FIMV_SI_CH0_SB_FRM_SIZE);
  297. s5p_mfc_write_shm(ctx, start_num_byte, START_BYTE_NUM);
  298. return 0;
  299. }
  300. /* Set decoding frame buffer */
  301. int s5p_mfc_set_dec_frame_buffer(struct s5p_mfc_ctx *ctx)
  302. {
  303. unsigned int frame_size, i;
  304. unsigned int frame_size_ch, frame_size_mv;
  305. struct s5p_mfc_dev *dev = ctx->dev;
  306. unsigned int dpb;
  307. size_t buf_addr1, buf_addr2;
  308. int buf_size1, buf_size2;
  309. buf_addr1 = ctx->bank1_phys;
  310. buf_size1 = ctx->bank1_size;
  311. buf_addr2 = ctx->bank2_phys;
  312. buf_size2 = ctx->bank2_size;
  313. dpb = mfc_read(dev, S5P_FIMV_SI_CH0_DPB_CONF_CTRL) &
  314. ~S5P_FIMV_DPB_COUNT_MASK;
  315. mfc_write(dev, ctx->total_dpb_count | dpb,
  316. S5P_FIMV_SI_CH0_DPB_CONF_CTRL);
  317. s5p_mfc_set_shared_buffer(ctx);
  318. switch (ctx->codec_mode) {
  319. case S5P_FIMV_CODEC_H264_DEC:
  320. mfc_write(dev, OFFSETA(buf_addr1),
  321. S5P_FIMV_H264_VERT_NB_MV_ADR);
  322. buf_addr1 += S5P_FIMV_DEC_VERT_NB_MV_SIZE;
  323. buf_size1 -= S5P_FIMV_DEC_VERT_NB_MV_SIZE;
  324. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H264_NB_IP_ADR);
  325. buf_addr1 += S5P_FIMV_DEC_NB_IP_SIZE;
  326. buf_size1 -= S5P_FIMV_DEC_NB_IP_SIZE;
  327. break;
  328. case S5P_FIMV_CODEC_MPEG4_DEC:
  329. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_MPEG4_NB_DCAC_ADR);
  330. buf_addr1 += S5P_FIMV_DEC_NB_DCAC_SIZE;
  331. buf_size1 -= S5P_FIMV_DEC_NB_DCAC_SIZE;
  332. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_MPEG4_UP_NB_MV_ADR);
  333. buf_addr1 += S5P_FIMV_DEC_UPNB_MV_SIZE;
  334. buf_size1 -= S5P_FIMV_DEC_UPNB_MV_SIZE;
  335. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_MPEG4_SA_MV_ADR);
  336. buf_addr1 += S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE;
  337. buf_size1 -= S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE;
  338. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_MPEG4_SP_ADR);
  339. buf_addr1 += S5P_FIMV_DEC_STX_PARSER_SIZE;
  340. buf_size1 -= S5P_FIMV_DEC_STX_PARSER_SIZE;
  341. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_MPEG4_OT_LINE_ADR);
  342. buf_addr1 += S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE;
  343. buf_size1 -= S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE;
  344. break;
  345. case S5P_FIMV_CODEC_H263_DEC:
  346. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H263_OT_LINE_ADR);
  347. buf_addr1 += S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE;
  348. buf_size1 -= S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE;
  349. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H263_UP_NB_MV_ADR);
  350. buf_addr1 += S5P_FIMV_DEC_UPNB_MV_SIZE;
  351. buf_size1 -= S5P_FIMV_DEC_UPNB_MV_SIZE;
  352. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H263_SA_MV_ADR);
  353. buf_addr1 += S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE;
  354. buf_size1 -= S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE;
  355. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H263_NB_DCAC_ADR);
  356. buf_addr1 += S5P_FIMV_DEC_NB_DCAC_SIZE;
  357. buf_size1 -= S5P_FIMV_DEC_NB_DCAC_SIZE;
  358. break;
  359. case S5P_FIMV_CODEC_VC1_DEC:
  360. case S5P_FIMV_CODEC_VC1RCV_DEC:
  361. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_NB_DCAC_ADR);
  362. buf_addr1 += S5P_FIMV_DEC_NB_DCAC_SIZE;
  363. buf_size1 -= S5P_FIMV_DEC_NB_DCAC_SIZE;
  364. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_OT_LINE_ADR);
  365. buf_addr1 += S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE;
  366. buf_size1 -= S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE;
  367. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_UP_NB_MV_ADR);
  368. buf_addr1 += S5P_FIMV_DEC_UPNB_MV_SIZE;
  369. buf_size1 -= S5P_FIMV_DEC_UPNB_MV_SIZE;
  370. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_SA_MV_ADR);
  371. buf_addr1 += S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE;
  372. buf_size1 -= S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE;
  373. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_BITPLANE3_ADR);
  374. buf_addr1 += S5P_FIMV_DEC_VC1_BITPLANE_SIZE;
  375. buf_size1 -= S5P_FIMV_DEC_VC1_BITPLANE_SIZE;
  376. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_BITPLANE2_ADR);
  377. buf_addr1 += S5P_FIMV_DEC_VC1_BITPLANE_SIZE;
  378. buf_size1 -= S5P_FIMV_DEC_VC1_BITPLANE_SIZE;
  379. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_BITPLANE1_ADR);
  380. buf_addr1 += S5P_FIMV_DEC_VC1_BITPLANE_SIZE;
  381. buf_size1 -= S5P_FIMV_DEC_VC1_BITPLANE_SIZE;
  382. break;
  383. case S5P_FIMV_CODEC_MPEG2_DEC:
  384. break;
  385. default:
  386. mfc_err("Unknown codec for decoding (%x)\n",
  387. ctx->codec_mode);
  388. return -EINVAL;
  389. break;
  390. }
  391. frame_size = ctx->luma_size;
  392. frame_size_ch = ctx->chroma_size;
  393. frame_size_mv = ctx->mv_size;
  394. mfc_debug(2, "Frm size: %d ch: %d mv: %d\n", frame_size, frame_size_ch,
  395. frame_size_mv);
  396. for (i = 0; i < ctx->total_dpb_count; i++) {
  397. /* Bank2 */
  398. mfc_debug(2, "Luma %d: %x\n", i,
  399. ctx->dst_bufs[i].cookie.raw.luma);
  400. mfc_write(dev, OFFSETB(ctx->dst_bufs[i].cookie.raw.luma),
  401. S5P_FIMV_DEC_LUMA_ADR + i * 4);
  402. mfc_debug(2, "\tChroma %d: %x\n", i,
  403. ctx->dst_bufs[i].cookie.raw.chroma);
  404. mfc_write(dev, OFFSETA(ctx->dst_bufs[i].cookie.raw.chroma),
  405. S5P_FIMV_DEC_CHROMA_ADR + i * 4);
  406. if (ctx->codec_mode == S5P_FIMV_CODEC_H264_DEC) {
  407. mfc_debug(2, "\tBuf2: %x, size: %d\n",
  408. buf_addr2, buf_size2);
  409. mfc_write(dev, OFFSETB(buf_addr2),
  410. S5P_FIMV_H264_MV_ADR + i * 4);
  411. buf_addr2 += frame_size_mv;
  412. buf_size2 -= frame_size_mv;
  413. }
  414. }
  415. mfc_debug(2, "Buf1: %u, buf_size1: %d\n", buf_addr1, buf_size1);
  416. mfc_debug(2, "Buf 1/2 size after: %d/%d (frames %d)\n",
  417. buf_size1, buf_size2, ctx->total_dpb_count);
  418. if (buf_size1 < 0 || buf_size2 < 0) {
  419. mfc_debug(2, "Not enough memory has been allocated\n");
  420. return -ENOMEM;
  421. }
  422. s5p_mfc_write_shm(ctx, frame_size, ALLOC_LUMA_DPB_SIZE);
  423. s5p_mfc_write_shm(ctx, frame_size_ch, ALLOC_CHROMA_DPB_SIZE);
  424. if (ctx->codec_mode == S5P_FIMV_CODEC_H264_DEC)
  425. s5p_mfc_write_shm(ctx, frame_size_mv, ALLOC_MV_SIZE);
  426. mfc_write(dev, ((S5P_FIMV_CH_INIT_BUFS & S5P_FIMV_CH_MASK)
  427. << S5P_FIMV_CH_SHIFT) | (ctx->inst_no),
  428. S5P_FIMV_SI_CH0_INST_ID);
  429. return 0;
  430. }
  431. /* Set registers for encoding stream buffer */
  432. int s5p_mfc_set_enc_stream_buffer(struct s5p_mfc_ctx *ctx,
  433. unsigned long addr, unsigned int size)
  434. {
  435. struct s5p_mfc_dev *dev = ctx->dev;
  436. mfc_write(dev, OFFSETA(addr), S5P_FIMV_ENC_SI_CH0_SB_ADR);
  437. mfc_write(dev, size, S5P_FIMV_ENC_SI_CH0_SB_SIZE);
  438. return 0;
  439. }
  440. void s5p_mfc_set_enc_frame_buffer(struct s5p_mfc_ctx *ctx,
  441. unsigned long y_addr, unsigned long c_addr)
  442. {
  443. struct s5p_mfc_dev *dev = ctx->dev;
  444. mfc_write(dev, OFFSETB(y_addr), S5P_FIMV_ENC_SI_CH0_CUR_Y_ADR);
  445. mfc_write(dev, OFFSETB(c_addr), S5P_FIMV_ENC_SI_CH0_CUR_C_ADR);
  446. }
  447. void s5p_mfc_get_enc_frame_buffer(struct s5p_mfc_ctx *ctx,
  448. unsigned long *y_addr, unsigned long *c_addr)
  449. {
  450. struct s5p_mfc_dev *dev = ctx->dev;
  451. *y_addr = dev->bank2 + (mfc_read(dev, S5P_FIMV_ENCODED_Y_ADDR)
  452. << MFC_OFFSET_SHIFT);
  453. *c_addr = dev->bank2 + (mfc_read(dev, S5P_FIMV_ENCODED_C_ADDR)
  454. << MFC_OFFSET_SHIFT);
  455. }
  456. /* Set encoding ref & codec buffer */
  457. int s5p_mfc_set_enc_ref_buffer(struct s5p_mfc_ctx *ctx)
  458. {
  459. struct s5p_mfc_dev *dev = ctx->dev;
  460. size_t buf_addr1, buf_addr2;
  461. size_t buf_size1, buf_size2;
  462. unsigned int enc_ref_y_size, enc_ref_c_size;
  463. unsigned int guard_width, guard_height;
  464. int i;
  465. buf_addr1 = ctx->bank1_phys;
  466. buf_size1 = ctx->bank1_size;
  467. buf_addr2 = ctx->bank2_phys;
  468. buf_size2 = ctx->bank2_size;
  469. enc_ref_y_size = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN)
  470. * ALIGN(ctx->img_height, S5P_FIMV_NV12MT_VALIGN);
  471. enc_ref_y_size = ALIGN(enc_ref_y_size, S5P_FIMV_NV12MT_SALIGN);
  472. if (ctx->codec_mode == S5P_FIMV_CODEC_H264_ENC) {
  473. enc_ref_c_size = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN)
  474. * ALIGN((ctx->img_height >> 1), S5P_FIMV_NV12MT_VALIGN);
  475. enc_ref_c_size = ALIGN(enc_ref_c_size, S5P_FIMV_NV12MT_SALIGN);
  476. } else {
  477. guard_width = ALIGN(ctx->img_width + 16,
  478. S5P_FIMV_NV12MT_HALIGN);
  479. guard_height = ALIGN((ctx->img_height >> 1) + 4,
  480. S5P_FIMV_NV12MT_VALIGN);
  481. enc_ref_c_size = ALIGN(guard_width * guard_height,
  482. S5P_FIMV_NV12MT_SALIGN);
  483. }
  484. mfc_debug(2, "buf_size1: %d, buf_size2: %d\n", buf_size1, buf_size2);
  485. switch (ctx->codec_mode) {
  486. case S5P_FIMV_CODEC_H264_ENC:
  487. for (i = 0; i < 2; i++) {
  488. mfc_write(dev, OFFSETA(buf_addr1),
  489. S5P_FIMV_ENC_REF0_LUMA_ADR + (4 * i));
  490. buf_addr1 += enc_ref_y_size;
  491. buf_size1 -= enc_ref_y_size;
  492. mfc_write(dev, OFFSETB(buf_addr2),
  493. S5P_FIMV_ENC_REF2_LUMA_ADR + (4 * i));
  494. buf_addr2 += enc_ref_y_size;
  495. buf_size2 -= enc_ref_y_size;
  496. }
  497. for (i = 0; i < 4; i++) {
  498. mfc_write(dev, OFFSETB(buf_addr2),
  499. S5P_FIMV_ENC_REF0_CHROMA_ADR + (4 * i));
  500. buf_addr2 += enc_ref_c_size;
  501. buf_size2 -= enc_ref_c_size;
  502. }
  503. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H264_UP_MV_ADR);
  504. buf_addr1 += S5P_FIMV_ENC_UPMV_SIZE;
  505. buf_size1 -= S5P_FIMV_ENC_UPMV_SIZE;
  506. mfc_write(dev, OFFSETA(buf_addr1),
  507. S5P_FIMV_H264_COZERO_FLAG_ADR);
  508. buf_addr1 += S5P_FIMV_ENC_COLFLG_SIZE;
  509. buf_size1 -= S5P_FIMV_ENC_COLFLG_SIZE;
  510. mfc_write(dev, OFFSETA(buf_addr1),
  511. S5P_FIMV_H264_UP_INTRA_MD_ADR);
  512. buf_addr1 += S5P_FIMV_ENC_INTRAMD_SIZE;
  513. buf_size1 -= S5P_FIMV_ENC_INTRAMD_SIZE;
  514. mfc_write(dev, OFFSETB(buf_addr2),
  515. S5P_FIMV_H264_UP_INTRA_PRED_ADR);
  516. buf_addr2 += S5P_FIMV_ENC_INTRAPRED_SIZE;
  517. buf_size2 -= S5P_FIMV_ENC_INTRAPRED_SIZE;
  518. mfc_write(dev, OFFSETA(buf_addr1),
  519. S5P_FIMV_H264_NBOR_INFO_ADR);
  520. buf_addr1 += S5P_FIMV_ENC_NBORINFO_SIZE;
  521. buf_size1 -= S5P_FIMV_ENC_NBORINFO_SIZE;
  522. mfc_debug(2, "buf_size1: %d, buf_size2: %d\n",
  523. buf_size1, buf_size2);
  524. break;
  525. case S5P_FIMV_CODEC_MPEG4_ENC:
  526. for (i = 0; i < 2; i++) {
  527. mfc_write(dev, OFFSETA(buf_addr1),
  528. S5P_FIMV_ENC_REF0_LUMA_ADR + (4 * i));
  529. buf_addr1 += enc_ref_y_size;
  530. buf_size1 -= enc_ref_y_size;
  531. mfc_write(dev, OFFSETB(buf_addr2),
  532. S5P_FIMV_ENC_REF2_LUMA_ADR + (4 * i));
  533. buf_addr2 += enc_ref_y_size;
  534. buf_size2 -= enc_ref_y_size;
  535. }
  536. for (i = 0; i < 4; i++) {
  537. mfc_write(dev, OFFSETB(buf_addr2),
  538. S5P_FIMV_ENC_REF0_CHROMA_ADR + (4 * i));
  539. buf_addr2 += enc_ref_c_size;
  540. buf_size2 -= enc_ref_c_size;
  541. }
  542. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_MPEG4_UP_MV_ADR);
  543. buf_addr1 += S5P_FIMV_ENC_UPMV_SIZE;
  544. buf_size1 -= S5P_FIMV_ENC_UPMV_SIZE;
  545. mfc_write(dev, OFFSETA(buf_addr1),
  546. S5P_FIMV_MPEG4_COZERO_FLAG_ADR);
  547. buf_addr1 += S5P_FIMV_ENC_COLFLG_SIZE;
  548. buf_size1 -= S5P_FIMV_ENC_COLFLG_SIZE;
  549. mfc_write(dev, OFFSETA(buf_addr1),
  550. S5P_FIMV_MPEG4_ACDC_COEF_ADR);
  551. buf_addr1 += S5P_FIMV_ENC_ACDCCOEF_SIZE;
  552. buf_size1 -= S5P_FIMV_ENC_ACDCCOEF_SIZE;
  553. mfc_debug(2, "buf_size1: %d, buf_size2: %d\n",
  554. buf_size1, buf_size2);
  555. break;
  556. case S5P_FIMV_CODEC_H263_ENC:
  557. for (i = 0; i < 2; i++) {
  558. mfc_write(dev, OFFSETA(buf_addr1),
  559. S5P_FIMV_ENC_REF0_LUMA_ADR + (4 * i));
  560. buf_addr1 += enc_ref_y_size;
  561. buf_size1 -= enc_ref_y_size;
  562. mfc_write(dev, OFFSETB(buf_addr2),
  563. S5P_FIMV_ENC_REF2_LUMA_ADR + (4 * i));
  564. buf_addr2 += enc_ref_y_size;
  565. buf_size2 -= enc_ref_y_size;
  566. }
  567. for (i = 0; i < 4; i++) {
  568. mfc_write(dev, OFFSETB(buf_addr2),
  569. S5P_FIMV_ENC_REF0_CHROMA_ADR + (4 * i));
  570. buf_addr2 += enc_ref_c_size;
  571. buf_size2 -= enc_ref_c_size;
  572. }
  573. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H263_UP_MV_ADR);
  574. buf_addr1 += S5P_FIMV_ENC_UPMV_SIZE;
  575. buf_size1 -= S5P_FIMV_ENC_UPMV_SIZE;
  576. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H263_ACDC_COEF_ADR);
  577. buf_addr1 += S5P_FIMV_ENC_ACDCCOEF_SIZE;
  578. buf_size1 -= S5P_FIMV_ENC_ACDCCOEF_SIZE;
  579. mfc_debug(2, "buf_size1: %d, buf_size2: %d\n",
  580. buf_size1, buf_size2);
  581. break;
  582. default:
  583. mfc_err("Unknown codec set for encoding: %d\n",
  584. ctx->codec_mode);
  585. return -EINVAL;
  586. }
  587. return 0;
  588. }
  589. static int s5p_mfc_set_enc_params(struct s5p_mfc_ctx *ctx)
  590. {
  591. struct s5p_mfc_dev *dev = ctx->dev;
  592. struct s5p_mfc_enc_params *p = &ctx->enc_params;
  593. unsigned int reg;
  594. unsigned int shm;
  595. /* width */
  596. mfc_write(dev, ctx->img_width, S5P_FIMV_ENC_HSIZE_PX);
  597. /* height */
  598. mfc_write(dev, ctx->img_height, S5P_FIMV_ENC_VSIZE_PX);
  599. /* pictype : enable, IDR period */
  600. reg = mfc_read(dev, S5P_FIMV_ENC_PIC_TYPE_CTRL);
  601. reg |= (1 << 18);
  602. reg &= ~(0xFFFF);
  603. reg |= p->gop_size;
  604. mfc_write(dev, reg, S5P_FIMV_ENC_PIC_TYPE_CTRL);
  605. mfc_write(dev, 0, S5P_FIMV_ENC_B_RECON_WRITE_ON);
  606. /* multi-slice control */
  607. /* multi-slice MB number or bit size */
  608. mfc_write(dev, p->slice_mode, S5P_FIMV_ENC_MSLICE_CTRL);
  609. if (p->slice_mode == V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB) {
  610. mfc_write(dev, p->slice_mb, S5P_FIMV_ENC_MSLICE_MB);
  611. } else if (p->slice_mode == V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES) {
  612. mfc_write(dev, p->slice_bit, S5P_FIMV_ENC_MSLICE_BIT);
  613. } else {
  614. mfc_write(dev, 0, S5P_FIMV_ENC_MSLICE_MB);
  615. mfc_write(dev, 0, S5P_FIMV_ENC_MSLICE_BIT);
  616. }
  617. /* cyclic intra refresh */
  618. mfc_write(dev, p->intra_refresh_mb, S5P_FIMV_ENC_CIR_CTRL);
  619. /* memory structure cur. frame */
  620. if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12M)
  621. mfc_write(dev, 0, S5P_FIMV_ENC_MAP_FOR_CUR);
  622. else if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12MT)
  623. mfc_write(dev, 3, S5P_FIMV_ENC_MAP_FOR_CUR);
  624. /* padding control & value */
  625. reg = mfc_read(dev, S5P_FIMV_ENC_PADDING_CTRL);
  626. if (p->pad) {
  627. /** enable */
  628. reg |= (1 << 31);
  629. /** cr value */
  630. reg &= ~(0xFF << 16);
  631. reg |= (p->pad_cr << 16);
  632. /** cb value */
  633. reg &= ~(0xFF << 8);
  634. reg |= (p->pad_cb << 8);
  635. /** y value */
  636. reg &= ~(0xFF);
  637. reg |= (p->pad_luma);
  638. } else {
  639. /** disable & all value clear */
  640. reg = 0;
  641. }
  642. mfc_write(dev, reg, S5P_FIMV_ENC_PADDING_CTRL);
  643. /* rate control config. */
  644. reg = mfc_read(dev, S5P_FIMV_ENC_RC_CONFIG);
  645. /** frame-level rate control */
  646. reg &= ~(0x1 << 9);
  647. reg |= (p->rc_frame << 9);
  648. mfc_write(dev, reg, S5P_FIMV_ENC_RC_CONFIG);
  649. /* bit rate */
  650. if (p->rc_frame)
  651. mfc_write(dev, p->rc_bitrate,
  652. S5P_FIMV_ENC_RC_BIT_RATE);
  653. else
  654. mfc_write(dev, 0, S5P_FIMV_ENC_RC_BIT_RATE);
  655. /* reaction coefficient */
  656. if (p->rc_frame)
  657. mfc_write(dev, p->rc_reaction_coeff, S5P_FIMV_ENC_RC_RPARA);
  658. shm = s5p_mfc_read_shm(ctx, EXT_ENC_CONTROL);
  659. /* seq header ctrl */
  660. shm &= ~(0x1 << 3);
  661. shm |= (p->seq_hdr_mode << 3);
  662. /* frame skip mode */
  663. shm &= ~(0x3 << 1);
  664. shm |= (p->frame_skip_mode << 1);
  665. s5p_mfc_write_shm(ctx, shm, EXT_ENC_CONTROL);
  666. /* fixed target bit */
  667. s5p_mfc_write_shm(ctx, p->fixed_target_bit, RC_CONTROL_CONFIG);
  668. return 0;
  669. }
  670. static int s5p_mfc_set_enc_params_h264(struct s5p_mfc_ctx *ctx)
  671. {
  672. struct s5p_mfc_dev *dev = ctx->dev;
  673. struct s5p_mfc_enc_params *p = &ctx->enc_params;
  674. struct s5p_mfc_h264_enc_params *p_264 = &p->codec.h264;
  675. unsigned int reg;
  676. unsigned int shm;
  677. s5p_mfc_set_enc_params(ctx);
  678. /* pictype : number of B */
  679. reg = mfc_read(dev, S5P_FIMV_ENC_PIC_TYPE_CTRL);
  680. /* num_b_frame - 0 ~ 2 */
  681. reg &= ~(0x3 << 16);
  682. reg |= (p->num_b_frame << 16);
  683. mfc_write(dev, reg, S5P_FIMV_ENC_PIC_TYPE_CTRL);
  684. /* profile & level */
  685. reg = mfc_read(dev, S5P_FIMV_ENC_PROFILE);
  686. /* level */
  687. reg &= ~(0xFF << 8);
  688. reg |= (p_264->level << 8);
  689. /* profile - 0 ~ 2 */
  690. reg &= ~(0x3F);
  691. reg |= p_264->profile;
  692. mfc_write(dev, reg, S5P_FIMV_ENC_PROFILE);
  693. /* interlace */
  694. mfc_write(dev, p->interlace, S5P_FIMV_ENC_PIC_STRUCT);
  695. /* height */
  696. if (p->interlace)
  697. mfc_write(dev, ctx->img_height >> 1, S5P_FIMV_ENC_VSIZE_PX);
  698. /* loopfilter ctrl */
  699. mfc_write(dev, p_264->loop_filter_mode, S5P_FIMV_ENC_LF_CTRL);
  700. /* loopfilter alpha offset */
  701. if (p_264->loop_filter_alpha < 0) {
  702. reg = 0x10;
  703. reg |= (0xFF - p_264->loop_filter_alpha) + 1;
  704. } else {
  705. reg = 0x00;
  706. reg |= (p_264->loop_filter_alpha & 0xF);
  707. }
  708. mfc_write(dev, reg, S5P_FIMV_ENC_ALPHA_OFF);
  709. /* loopfilter beta offset */
  710. if (p_264->loop_filter_beta < 0) {
  711. reg = 0x10;
  712. reg |= (0xFF - p_264->loop_filter_beta) + 1;
  713. } else {
  714. reg = 0x00;
  715. reg |= (p_264->loop_filter_beta & 0xF);
  716. }
  717. mfc_write(dev, reg, S5P_FIMV_ENC_BETA_OFF);
  718. /* entropy coding mode */
  719. if (p_264->entropy_mode == V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC)
  720. mfc_write(dev, 1, S5P_FIMV_ENC_H264_ENTROPY_MODE);
  721. else
  722. mfc_write(dev, 0, S5P_FIMV_ENC_H264_ENTROPY_MODE);
  723. /* number of ref. picture */
  724. reg = mfc_read(dev, S5P_FIMV_ENC_H264_NUM_OF_REF);
  725. /* num of ref. pictures of P */
  726. reg &= ~(0x3 << 5);
  727. reg |= (p_264->num_ref_pic_4p << 5);
  728. /* max number of ref. pictures */
  729. reg &= ~(0x1F);
  730. reg |= p_264->max_ref_pic;
  731. mfc_write(dev, reg, S5P_FIMV_ENC_H264_NUM_OF_REF);
  732. /* 8x8 transform enable */
  733. mfc_write(dev, p_264->_8x8_transform, S5P_FIMV_ENC_H264_TRANS_FLAG);
  734. /* rate control config. */
  735. reg = mfc_read(dev, S5P_FIMV_ENC_RC_CONFIG);
  736. /* macroblock level rate control */
  737. reg &= ~(0x1 << 8);
  738. reg |= (p_264->rc_mb << 8);
  739. /* frame QP */
  740. reg &= ~(0x3F);
  741. reg |= p_264->rc_frame_qp;
  742. mfc_write(dev, reg, S5P_FIMV_ENC_RC_CONFIG);
  743. /* frame rate */
  744. if (p->rc_frame && p->rc_framerate_denom)
  745. mfc_write(dev, p->rc_framerate_num * 1000
  746. / p->rc_framerate_denom, S5P_FIMV_ENC_RC_FRAME_RATE);
  747. else
  748. mfc_write(dev, 0, S5P_FIMV_ENC_RC_FRAME_RATE);
  749. /* max & min value of QP */
  750. reg = mfc_read(dev, S5P_FIMV_ENC_RC_QBOUND);
  751. /* max QP */
  752. reg &= ~(0x3F << 8);
  753. reg |= (p_264->rc_max_qp << 8);
  754. /* min QP */
  755. reg &= ~(0x3F);
  756. reg |= p_264->rc_min_qp;
  757. mfc_write(dev, reg, S5P_FIMV_ENC_RC_QBOUND);
  758. /* macroblock adaptive scaling features */
  759. if (p_264->rc_mb) {
  760. reg = mfc_read(dev, S5P_FIMV_ENC_RC_MB_CTRL);
  761. /* dark region */
  762. reg &= ~(0x1 << 3);
  763. reg |= (p_264->rc_mb_dark << 3);
  764. /* smooth region */
  765. reg &= ~(0x1 << 2);
  766. reg |= (p_264->rc_mb_smooth << 2);
  767. /* static region */
  768. reg &= ~(0x1 << 1);
  769. reg |= (p_264->rc_mb_static << 1);
  770. /* high activity region */
  771. reg &= ~(0x1);
  772. reg |= p_264->rc_mb_activity;
  773. mfc_write(dev, reg, S5P_FIMV_ENC_RC_MB_CTRL);
  774. }
  775. if (!p->rc_frame &&
  776. !p_264->rc_mb) {
  777. shm = s5p_mfc_read_shm(ctx, P_B_FRAME_QP);
  778. shm &= ~(0xFFF);
  779. shm |= ((p_264->rc_b_frame_qp & 0x3F) << 6);
  780. shm |= (p_264->rc_p_frame_qp & 0x3F);
  781. s5p_mfc_write_shm(ctx, shm, P_B_FRAME_QP);
  782. }
  783. /* extended encoder ctrl */
  784. shm = s5p_mfc_read_shm(ctx, EXT_ENC_CONTROL);
  785. /* AR VUI control */
  786. shm &= ~(0x1 << 15);
  787. shm |= (p_264->vui_sar << 1);
  788. s5p_mfc_write_shm(ctx, shm, EXT_ENC_CONTROL);
  789. if (p_264->vui_sar) {
  790. /* aspect ration IDC */
  791. shm = s5p_mfc_read_shm(ctx, SAMPLE_ASPECT_RATIO_IDC);
  792. shm &= ~(0xFF);
  793. shm |= p_264->vui_sar_idc;
  794. s5p_mfc_write_shm(ctx, shm, SAMPLE_ASPECT_RATIO_IDC);
  795. if (p_264->vui_sar_idc == 0xFF) {
  796. /* sample AR info */
  797. shm = s5p_mfc_read_shm(ctx, EXTENDED_SAR);
  798. shm &= ~(0xFFFFFFFF);
  799. shm |= p_264->vui_ext_sar_width << 16;
  800. shm |= p_264->vui_ext_sar_height;
  801. s5p_mfc_write_shm(ctx, shm, EXTENDED_SAR);
  802. }
  803. }
  804. /* intra picture period for H.264 */
  805. shm = s5p_mfc_read_shm(ctx, H264_I_PERIOD);
  806. /* control */
  807. shm &= ~(0x1 << 16);
  808. shm |= (p_264->open_gop << 16);
  809. /* value */
  810. if (p_264->open_gop) {
  811. shm &= ~(0xFFFF);
  812. shm |= p_264->open_gop_size;
  813. }
  814. s5p_mfc_write_shm(ctx, shm, H264_I_PERIOD);
  815. /* extended encoder ctrl */
  816. shm = s5p_mfc_read_shm(ctx, EXT_ENC_CONTROL);
  817. /* vbv buffer size */
  818. if (p->frame_skip_mode ==
  819. V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT) {
  820. shm &= ~(0xFFFF << 16);
  821. shm |= (p_264->cpb_size << 16);
  822. }
  823. s5p_mfc_write_shm(ctx, shm, EXT_ENC_CONTROL);
  824. return 0;
  825. }
  826. static int s5p_mfc_set_enc_params_mpeg4(struct s5p_mfc_ctx *ctx)
  827. {
  828. struct s5p_mfc_dev *dev = ctx->dev;
  829. struct s5p_mfc_enc_params *p = &ctx->enc_params;
  830. struct s5p_mfc_mpeg4_enc_params *p_mpeg4 = &p->codec.mpeg4;
  831. unsigned int reg;
  832. unsigned int shm;
  833. unsigned int framerate;
  834. s5p_mfc_set_enc_params(ctx);
  835. /* pictype : number of B */
  836. reg = mfc_read(dev, S5P_FIMV_ENC_PIC_TYPE_CTRL);
  837. /* num_b_frame - 0 ~ 2 */
  838. reg &= ~(0x3 << 16);
  839. reg |= (p->num_b_frame << 16);
  840. mfc_write(dev, reg, S5P_FIMV_ENC_PIC_TYPE_CTRL);
  841. /* profile & level */
  842. reg = mfc_read(dev, S5P_FIMV_ENC_PROFILE);
  843. /* level */
  844. reg &= ~(0xFF << 8);
  845. reg |= (p_mpeg4->level << 8);
  846. /* profile - 0 ~ 2 */
  847. reg &= ~(0x3F);
  848. reg |= p_mpeg4->profile;
  849. mfc_write(dev, reg, S5P_FIMV_ENC_PROFILE);
  850. /* quarter_pixel */
  851. mfc_write(dev, p_mpeg4->quarter_pixel, S5P_FIMV_ENC_MPEG4_QUART_PXL);
  852. /* qp */
  853. if (!p->rc_frame) {
  854. shm = s5p_mfc_read_shm(ctx, P_B_FRAME_QP);
  855. shm &= ~(0xFFF);
  856. shm |= ((p_mpeg4->rc_b_frame_qp & 0x3F) << 6);
  857. shm |= (p_mpeg4->rc_p_frame_qp & 0x3F);
  858. s5p_mfc_write_shm(ctx, shm, P_B_FRAME_QP);
  859. }
  860. /* frame rate */
  861. if (p->rc_frame) {
  862. if (p->rc_framerate_denom > 0) {
  863. framerate = p->rc_framerate_num * 1000 /
  864. p->rc_framerate_denom;
  865. mfc_write(dev, framerate,
  866. S5P_FIMV_ENC_RC_FRAME_RATE);
  867. shm = s5p_mfc_read_shm(ctx, RC_VOP_TIMING);
  868. shm &= ~(0xFFFFFFFF);
  869. shm |= (1 << 31);
  870. shm |= ((p->rc_framerate_num & 0x7FFF) << 16);
  871. shm |= (p->rc_framerate_denom & 0xFFFF);
  872. s5p_mfc_write_shm(ctx, shm, RC_VOP_TIMING);
  873. }
  874. } else {
  875. mfc_write(dev, 0, S5P_FIMV_ENC_RC_FRAME_RATE);
  876. }
  877. /* rate control config. */
  878. reg = mfc_read(dev, S5P_FIMV_ENC_RC_CONFIG);
  879. /* frame QP */
  880. reg &= ~(0x3F);
  881. reg |= p_mpeg4->rc_frame_qp;
  882. mfc_write(dev, reg, S5P_FIMV_ENC_RC_CONFIG);
  883. /* max & min value of QP */
  884. reg = mfc_read(dev, S5P_FIMV_ENC_RC_QBOUND);
  885. /* max QP */
  886. reg &= ~(0x3F << 8);
  887. reg |= (p_mpeg4->rc_max_qp << 8);
  888. /* min QP */
  889. reg &= ~(0x3F);
  890. reg |= p_mpeg4->rc_min_qp;
  891. mfc_write(dev, reg, S5P_FIMV_ENC_RC_QBOUND);
  892. /* extended encoder ctrl */
  893. shm = s5p_mfc_read_shm(ctx, EXT_ENC_CONTROL);
  894. /* vbv buffer size */
  895. if (p->frame_skip_mode ==
  896. V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT) {
  897. shm &= ~(0xFFFF << 16);
  898. shm |= (p->vbv_size << 16);
  899. }
  900. s5p_mfc_write_shm(ctx, shm, EXT_ENC_CONTROL);
  901. return 0;
  902. }
  903. static int s5p_mfc_set_enc_params_h263(struct s5p_mfc_ctx *ctx)
  904. {
  905. struct s5p_mfc_dev *dev = ctx->dev;
  906. struct s5p_mfc_enc_params *p = &ctx->enc_params;
  907. struct s5p_mfc_mpeg4_enc_params *p_h263 = &p->codec.mpeg4;
  908. unsigned int reg;
  909. unsigned int shm;
  910. s5p_mfc_set_enc_params(ctx);
  911. /* qp */
  912. if (!p->rc_frame) {
  913. shm = s5p_mfc_read_shm(ctx, P_B_FRAME_QP);
  914. shm &= ~(0xFFF);
  915. shm |= (p_h263->rc_p_frame_qp & 0x3F);
  916. s5p_mfc_write_shm(ctx, shm, P_B_FRAME_QP);
  917. }
  918. /* frame rate */
  919. if (p->rc_frame && p->rc_framerate_denom)
  920. mfc_write(dev, p->rc_framerate_num * 1000
  921. / p->rc_framerate_denom, S5P_FIMV_ENC_RC_FRAME_RATE);
  922. else
  923. mfc_write(dev, 0, S5P_FIMV_ENC_RC_FRAME_RATE);
  924. /* rate control config. */
  925. reg = mfc_read(dev, S5P_FIMV_ENC_RC_CONFIG);
  926. /* frame QP */
  927. reg &= ~(0x3F);
  928. reg |= p_h263->rc_frame_qp;
  929. mfc_write(dev, reg, S5P_FIMV_ENC_RC_CONFIG);
  930. /* max & min value of QP */
  931. reg = mfc_read(dev, S5P_FIMV_ENC_RC_QBOUND);
  932. /* max QP */
  933. reg &= ~(0x3F << 8);
  934. reg |= (p_h263->rc_max_qp << 8);
  935. /* min QP */
  936. reg &= ~(0x3F);
  937. reg |= p_h263->rc_min_qp;
  938. mfc_write(dev, reg, S5P_FIMV_ENC_RC_QBOUND);
  939. /* extended encoder ctrl */
  940. shm = s5p_mfc_read_shm(ctx, EXT_ENC_CONTROL);
  941. /* vbv buffer size */
  942. if (p->frame_skip_mode ==
  943. V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT) {
  944. shm &= ~(0xFFFF << 16);
  945. shm |= (p->vbv_size << 16);
  946. }
  947. s5p_mfc_write_shm(ctx, shm, EXT_ENC_CONTROL);
  948. return 0;
  949. }
  950. /* Initialize decoding */
  951. int s5p_mfc_init_decode(struct s5p_mfc_ctx *ctx)
  952. {
  953. struct s5p_mfc_dev *dev = ctx->dev;
  954. s5p_mfc_set_shared_buffer(ctx);
  955. /* Setup loop filter, for decoding this is only valid for MPEG4 */
  956. if (ctx->codec_mode == S5P_FIMV_CODEC_MPEG4_DEC)
  957. mfc_write(dev, ctx->loop_filter_mpeg4, S5P_FIMV_ENC_LF_CTRL);
  958. else
  959. mfc_write(dev, 0, S5P_FIMV_ENC_LF_CTRL);
  960. mfc_write(dev, ((ctx->slice_interface & S5P_FIMV_SLICE_INT_MASK) <<
  961. S5P_FIMV_SLICE_INT_SHIFT) | (ctx->display_delay_enable <<
  962. S5P_FIMV_DDELAY_ENA_SHIFT) | ((ctx->display_delay &
  963. S5P_FIMV_DDELAY_VAL_MASK) << S5P_FIMV_DDELAY_VAL_SHIFT),
  964. S5P_FIMV_SI_CH0_DPB_CONF_CTRL);
  965. mfc_write(dev,
  966. ((S5P_FIMV_CH_SEQ_HEADER & S5P_FIMV_CH_MASK) << S5P_FIMV_CH_SHIFT)
  967. | (ctx->inst_no), S5P_FIMV_SI_CH0_INST_ID);
  968. return 0;
  969. }
  970. static void s5p_mfc_set_flush(struct s5p_mfc_ctx *ctx, int flush)
  971. {
  972. struct s5p_mfc_dev *dev = ctx->dev;
  973. unsigned int dpb;
  974. if (flush)
  975. dpb = mfc_read(dev, S5P_FIMV_SI_CH0_DPB_CONF_CTRL) | (
  976. S5P_FIMV_DPB_FLUSH_MASK << S5P_FIMV_DPB_FLUSH_SHIFT);
  977. else
  978. dpb = mfc_read(dev, S5P_FIMV_SI_CH0_DPB_CONF_CTRL) &
  979. ~(S5P_FIMV_DPB_FLUSH_MASK << S5P_FIMV_DPB_FLUSH_SHIFT);
  980. mfc_write(dev, dpb, S5P_FIMV_SI_CH0_DPB_CONF_CTRL);
  981. }
  982. /* Decode a single frame */
  983. int s5p_mfc_decode_one_frame(struct s5p_mfc_ctx *ctx,
  984. enum s5p_mfc_decode_arg last_frame)
  985. {
  986. struct s5p_mfc_dev *dev = ctx->dev;
  987. mfc_write(dev, ctx->dec_dst_flag, S5P_FIMV_SI_CH0_RELEASE_BUF);
  988. s5p_mfc_set_shared_buffer(ctx);
  989. s5p_mfc_set_flush(ctx, ctx->dpb_flush_flag);
  990. /* Issue different commands to instance basing on whether it
  991. * is the last frame or not. */
  992. switch (last_frame) {
  993. case MFC_DEC_FRAME:
  994. mfc_write(dev, ((S5P_FIMV_CH_FRAME_START & S5P_FIMV_CH_MASK) <<
  995. S5P_FIMV_CH_SHIFT) | (ctx->inst_no), S5P_FIMV_SI_CH0_INST_ID);
  996. break;
  997. case MFC_DEC_LAST_FRAME:
  998. mfc_write(dev, ((S5P_FIMV_CH_LAST_FRAME & S5P_FIMV_CH_MASK) <<
  999. S5P_FIMV_CH_SHIFT) | (ctx->inst_no), S5P_FIMV_SI_CH0_INST_ID);
  1000. break;
  1001. case MFC_DEC_RES_CHANGE:
  1002. mfc_write(dev, ((S5P_FIMV_CH_FRAME_START_REALLOC &
  1003. S5P_FIMV_CH_MASK) << S5P_FIMV_CH_SHIFT) | (ctx->inst_no),
  1004. S5P_FIMV_SI_CH0_INST_ID);
  1005. break;
  1006. }
  1007. mfc_debug(2, "Decoding a usual frame\n");
  1008. return 0;
  1009. }
  1010. int s5p_mfc_init_encode(struct s5p_mfc_ctx *ctx)
  1011. {
  1012. struct s5p_mfc_dev *dev = ctx->dev;
  1013. if (ctx->codec_mode == S5P_FIMV_CODEC_H264_ENC)
  1014. s5p_mfc_set_enc_params_h264(ctx);
  1015. else if (ctx->codec_mode == S5P_FIMV_CODEC_MPEG4_ENC)
  1016. s5p_mfc_set_enc_params_mpeg4(ctx);
  1017. else if (ctx->codec_mode == S5P_FIMV_CODEC_H263_ENC)
  1018. s5p_mfc_set_enc_params_h263(ctx);
  1019. else {
  1020. mfc_err("Unknown codec for encoding (%x)\n",
  1021. ctx->codec_mode);
  1022. return -EINVAL;
  1023. }
  1024. s5p_mfc_set_shared_buffer(ctx);
  1025. mfc_write(dev, ((S5P_FIMV_CH_SEQ_HEADER << 16) & 0x70000) |
  1026. (ctx->inst_no), S5P_FIMV_SI_CH0_INST_ID);
  1027. return 0;
  1028. }
  1029. /* Encode a single frame */
  1030. int s5p_mfc_encode_one_frame(struct s5p_mfc_ctx *ctx)
  1031. {
  1032. struct s5p_mfc_dev *dev = ctx->dev;
  1033. /* memory structure cur. frame */
  1034. if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12M)
  1035. mfc_write(dev, 0, S5P_FIMV_ENC_MAP_FOR_CUR);
  1036. else if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12MT)
  1037. mfc_write(dev, 3, S5P_FIMV_ENC_MAP_FOR_CUR);
  1038. s5p_mfc_set_shared_buffer(ctx);
  1039. mfc_write(dev, (S5P_FIMV_CH_FRAME_START << 16 & 0x70000) |
  1040. (ctx->inst_no), S5P_FIMV_SI_CH0_INST_ID);
  1041. return 0;
  1042. }
  1043. static int s5p_mfc_get_new_ctx(struct s5p_mfc_dev *dev)
  1044. {
  1045. unsigned long flags;
  1046. int new_ctx;
  1047. int cnt;
  1048. spin_lock_irqsave(&dev->condlock, flags);
  1049. new_ctx = (dev->curr_ctx + 1) % MFC_NUM_CONTEXTS;
  1050. cnt = 0;
  1051. while (!test_bit(new_ctx, &dev->ctx_work_bits)) {
  1052. new_ctx = (new_ctx + 1) % MFC_NUM_CONTEXTS;
  1053. if (++cnt > MFC_NUM_CONTEXTS) {
  1054. /* No contexts to run */
  1055. spin_unlock_irqrestore(&dev->condlock, flags);
  1056. return -EAGAIN;
  1057. }
  1058. }
  1059. spin_unlock_irqrestore(&dev->condlock, flags);
  1060. return new_ctx;
  1061. }
  1062. static void s5p_mfc_run_res_change(struct s5p_mfc_ctx *ctx)
  1063. {
  1064. struct s5p_mfc_dev *dev = ctx->dev;
  1065. s5p_mfc_set_dec_stream_buffer(ctx, 0, 0, 0);
  1066. dev->curr_ctx = ctx->num;
  1067. s5p_mfc_clean_ctx_int_flags(ctx);
  1068. s5p_mfc_decode_one_frame(ctx, MFC_DEC_RES_CHANGE);
  1069. }
  1070. static int s5p_mfc_run_dec_frame(struct s5p_mfc_ctx *ctx, int last_frame)
  1071. {
  1072. struct s5p_mfc_dev *dev = ctx->dev;
  1073. struct s5p_mfc_buf *temp_vb;
  1074. unsigned long flags;
  1075. unsigned int index;
  1076. spin_lock_irqsave(&dev->irqlock, flags);
  1077. /* Frames are being decoded */
  1078. if (list_empty(&ctx->src_queue)) {
  1079. mfc_debug(2, "No src buffers\n");
  1080. spin_unlock_irqrestore(&dev->irqlock, flags);
  1081. return -EAGAIN;
  1082. }
  1083. /* Get the next source buffer */
  1084. temp_vb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
  1085. temp_vb->used = 1;
  1086. s5p_mfc_set_dec_stream_buffer(ctx,
  1087. vb2_dma_contig_plane_dma_addr(temp_vb->b, 0), ctx->consumed_stream,
  1088. temp_vb->b->v4l2_planes[0].bytesused);
  1089. spin_unlock_irqrestore(&dev->irqlock, flags);
  1090. index = temp_vb->b->v4l2_buf.index;
  1091. dev->curr_ctx = ctx->num;
  1092. s5p_mfc_clean_ctx_int_flags(ctx);
  1093. if (temp_vb->b->v4l2_planes[0].bytesused == 0) {
  1094. last_frame = MFC_DEC_LAST_FRAME;
  1095. mfc_debug(2, "Setting ctx->state to FINISHING\n");
  1096. ctx->state = MFCINST_FINISHING;
  1097. }
  1098. s5p_mfc_decode_one_frame(ctx, last_frame);
  1099. return 0;
  1100. }
  1101. static int s5p_mfc_run_enc_frame(struct s5p_mfc_ctx *ctx)
  1102. {
  1103. struct s5p_mfc_dev *dev = ctx->dev;
  1104. unsigned long flags;
  1105. struct s5p_mfc_buf *dst_mb;
  1106. struct s5p_mfc_buf *src_mb;
  1107. unsigned long src_y_addr, src_c_addr, dst_addr;
  1108. unsigned int dst_size;
  1109. spin_lock_irqsave(&dev->irqlock, flags);
  1110. if (list_empty(&ctx->src_queue)) {
  1111. mfc_debug(2, "no src buffers\n");
  1112. spin_unlock_irqrestore(&dev->irqlock, flags);
  1113. return -EAGAIN;
  1114. }
  1115. if (list_empty(&ctx->dst_queue)) {
  1116. mfc_debug(2, "no dst buffers\n");
  1117. spin_unlock_irqrestore(&dev->irqlock, flags);
  1118. return -EAGAIN;
  1119. }
  1120. src_mb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
  1121. src_mb->used = 1;
  1122. src_y_addr = vb2_dma_contig_plane_dma_addr(src_mb->b, 0);
  1123. src_c_addr = vb2_dma_contig_plane_dma_addr(src_mb->b, 1);
  1124. s5p_mfc_set_enc_frame_buffer(ctx, src_y_addr, src_c_addr);
  1125. dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
  1126. dst_mb->used = 1;
  1127. dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->b, 0);
  1128. dst_size = vb2_plane_size(dst_mb->b, 0);
  1129. s5p_mfc_set_enc_stream_buffer(ctx, dst_addr, dst_size);
  1130. spin_unlock_irqrestore(&dev->irqlock, flags);
  1131. dev->curr_ctx = ctx->num;
  1132. s5p_mfc_clean_ctx_int_flags(ctx);
  1133. s5p_mfc_encode_one_frame(ctx);
  1134. return 0;
  1135. }
  1136. static void s5p_mfc_run_init_dec(struct s5p_mfc_ctx *ctx)
  1137. {
  1138. struct s5p_mfc_dev *dev = ctx->dev;
  1139. unsigned long flags;
  1140. struct s5p_mfc_buf *temp_vb;
  1141. /* Initializing decoding - parsing header */
  1142. spin_lock_irqsave(&dev->irqlock, flags);
  1143. mfc_debug(2, "Preparing to init decoding\n");
  1144. temp_vb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
  1145. s5p_mfc_set_dec_desc_buffer(ctx);
  1146. mfc_debug(2, "Header size: %d\n", temp_vb->b->v4l2_planes[0].bytesused);
  1147. s5p_mfc_set_dec_stream_buffer(ctx,
  1148. vb2_dma_contig_plane_dma_addr(temp_vb->b, 0),
  1149. 0, temp_vb->b->v4l2_planes[0].bytesused);
  1150. spin_unlock_irqrestore(&dev->irqlock, flags);
  1151. dev->curr_ctx = ctx->num;
  1152. s5p_mfc_clean_ctx_int_flags(ctx);
  1153. s5p_mfc_init_decode(ctx);
  1154. }
  1155. static void s5p_mfc_run_init_enc(struct s5p_mfc_ctx *ctx)
  1156. {
  1157. struct s5p_mfc_dev *dev = ctx->dev;
  1158. unsigned long flags;
  1159. struct s5p_mfc_buf *dst_mb;
  1160. unsigned long dst_addr;
  1161. unsigned int dst_size;
  1162. s5p_mfc_set_enc_ref_buffer(ctx);
  1163. spin_lock_irqsave(&dev->irqlock, flags);
  1164. dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
  1165. dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->b, 0);
  1166. dst_size = vb2_plane_size(dst_mb->b, 0);
  1167. s5p_mfc_set_enc_stream_buffer(ctx, dst_addr, dst_size);
  1168. spin_unlock_irqrestore(&dev->irqlock, flags);
  1169. dev->curr_ctx = ctx->num;
  1170. s5p_mfc_clean_ctx_int_flags(ctx);
  1171. s5p_mfc_init_encode(ctx);
  1172. }
  1173. static int s5p_mfc_run_init_dec_buffers(struct s5p_mfc_ctx *ctx)
  1174. {
  1175. struct s5p_mfc_dev *dev = ctx->dev;
  1176. unsigned long flags;
  1177. struct s5p_mfc_buf *temp_vb;
  1178. int ret;
  1179. /*
  1180. * Header was parsed now starting processing
  1181. * First set the output frame buffers
  1182. */
  1183. if (ctx->capture_state != QUEUE_BUFS_MMAPED) {
  1184. mfc_err("It seems that not all destionation buffers were "
  1185. "mmaped\nMFC requires that all destination are mmaped "
  1186. "before starting processing\n");
  1187. return -EAGAIN;
  1188. }
  1189. spin_lock_irqsave(&dev->irqlock, flags);
  1190. if (list_empty(&ctx->src_queue)) {
  1191. mfc_err("Header has been deallocated in the middle of"
  1192. " initialization\n");
  1193. spin_unlock_irqrestore(&dev->irqlock, flags);
  1194. return -EIO;
  1195. }
  1196. temp_vb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
  1197. mfc_debug(2, "Header size: %d\n", temp_vb->b->v4l2_planes[0].bytesused);
  1198. s5p_mfc_set_dec_stream_buffer(ctx,
  1199. vb2_dma_contig_plane_dma_addr(temp_vb->b, 0),
  1200. 0, temp_vb->b->v4l2_planes[0].bytesused);
  1201. spin_unlock_irqrestore(&dev->irqlock, flags);
  1202. dev->curr_ctx = ctx->num;
  1203. s5p_mfc_clean_ctx_int_flags(ctx);
  1204. ret = s5p_mfc_set_dec_frame_buffer(ctx);
  1205. if (ret) {
  1206. mfc_err("Failed to alloc frame mem\n");
  1207. ctx->state = MFCINST_ERROR;
  1208. }
  1209. return ret;
  1210. }
  1211. /* Try running an operation on hardware */
  1212. void s5p_mfc_try_run(struct s5p_mfc_dev *dev)
  1213. {
  1214. struct s5p_mfc_ctx *ctx;
  1215. int new_ctx;
  1216. unsigned int ret = 0;
  1217. if (test_bit(0, &dev->enter_suspend)) {
  1218. mfc_debug(1, "Entering suspend so do not schedule any jobs\n");
  1219. return;
  1220. }
  1221. /* Check whether hardware is not running */
  1222. if (test_and_set_bit(0, &dev->hw_lock) != 0) {
  1223. /* This is perfectly ok, the scheduled ctx should wait */
  1224. mfc_debug(1, "Couldn't lock HW\n");
  1225. return;
  1226. }
  1227. /* Choose the context to run */
  1228. new_ctx = s5p_mfc_get_new_ctx(dev);
  1229. if (new_ctx < 0) {
  1230. /* No contexts to run */
  1231. if (test_and_clear_bit(0, &dev->hw_lock) == 0) {
  1232. mfc_err("Failed to unlock hardware\n");
  1233. return;
  1234. }
  1235. mfc_debug(1, "No ctx is scheduled to be run\n");
  1236. return;
  1237. }
  1238. ctx = dev->ctx[new_ctx];
  1239. /* Got context to run in ctx */
  1240. /*
  1241. * Last frame has already been sent to MFC.
  1242. * Now obtaining frames from MFC buffer
  1243. */
  1244. s5p_mfc_clock_on();
  1245. if (ctx->type == MFCINST_DECODER) {
  1246. s5p_mfc_set_dec_desc_buffer(ctx);
  1247. switch (ctx->state) {
  1248. case MFCINST_FINISHING:
  1249. s5p_mfc_run_dec_frame(ctx, MFC_DEC_LAST_FRAME);
  1250. break;
  1251. case MFCINST_RUNNING:
  1252. ret = s5p_mfc_run_dec_frame(ctx, MFC_DEC_FRAME);
  1253. break;
  1254. case MFCINST_INIT:
  1255. s5p_mfc_clean_ctx_int_flags(ctx);
  1256. ret = s5p_mfc_open_inst_cmd(ctx);
  1257. break;
  1258. case MFCINST_RETURN_INST:
  1259. s5p_mfc_clean_ctx_int_flags(ctx);
  1260. ret = s5p_mfc_close_inst_cmd(ctx);
  1261. break;
  1262. case MFCINST_GOT_INST:
  1263. s5p_mfc_run_init_dec(ctx);
  1264. break;
  1265. case MFCINST_HEAD_PARSED:
  1266. ret = s5p_mfc_run_init_dec_buffers(ctx);
  1267. mfc_debug(1, "head parsed\n");
  1268. break;
  1269. case MFCINST_RES_CHANGE_INIT:
  1270. s5p_mfc_run_res_change(ctx);
  1271. break;
  1272. case MFCINST_RES_CHANGE_FLUSH:
  1273. s5p_mfc_run_dec_frame(ctx, MFC_DEC_FRAME);
  1274. break;
  1275. case MFCINST_RES_CHANGE_END:
  1276. mfc_debug(2, "Finished remaining frames after resolution change\n");
  1277. ctx->capture_state = QUEUE_FREE;
  1278. mfc_debug(2, "Will re-init the codec\n");
  1279. s5p_mfc_run_init_dec(ctx);
  1280. break;
  1281. default:
  1282. ret = -EAGAIN;
  1283. }
  1284. } else if (ctx->type == MFCINST_ENCODER) {
  1285. switch (ctx->state) {
  1286. case MFCINST_FINISHING:
  1287. case MFCINST_RUNNING:
  1288. ret = s5p_mfc_run_enc_frame(ctx);
  1289. break;
  1290. case MFCINST_INIT:
  1291. s5p_mfc_clean_ctx_int_flags(ctx);
  1292. ret = s5p_mfc_open_inst_cmd(ctx);
  1293. break;
  1294. case MFCINST_RETURN_INST:
  1295. s5p_mfc_clean_ctx_int_flags(ctx);
  1296. ret = s5p_mfc_close_inst_cmd(ctx);
  1297. break;
  1298. case MFCINST_GOT_INST:
  1299. s5p_mfc_run_init_enc(ctx);
  1300. break;
  1301. default:
  1302. ret = -EAGAIN;
  1303. }
  1304. } else {
  1305. mfc_err("Invalid context type: %d\n", ctx->type);
  1306. ret = -EAGAIN;
  1307. }
  1308. if (ret) {
  1309. /* Free hardware lock */
  1310. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  1311. mfc_err("Failed to unlock hardware\n");
  1312. /* This is in deed imporant, as no operation has been
  1313. * scheduled, reduce the clock count as no one will
  1314. * ever do this, because no interrupt related to this try_run
  1315. * will ever come from hardware. */
  1316. s5p_mfc_clock_off();
  1317. }
  1318. }
  1319. void s5p_mfc_cleanup_queue(struct list_head *lh, struct vb2_queue *vq)
  1320. {
  1321. struct s5p_mfc_buf *b;
  1322. int i;
  1323. while (!list_empty(lh)) {
  1324. b = list_entry(lh->next, struct s5p_mfc_buf, list);
  1325. for (i = 0; i < b->b->num_planes; i++)
  1326. vb2_set_plane_payload(b->b, i, 0);
  1327. vb2_buffer_done(b->b, VB2_BUF_STATE_ERROR);
  1328. list_del(&b->list);
  1329. }
  1330. }