s5p_mfc_opr_v5.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  1. /*
  2. * drivers/media/platform/samsung/mfc5/s5p_mfc_opr_v5.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 "s5p_mfc_common.h"
  15. #include "s5p_mfc_cmd.h"
  16. #include "s5p_mfc_ctrl.h"
  17. #include "s5p_mfc_debug.h"
  18. #include "s5p_mfc_intr.h"
  19. #include "s5p_mfc_pm.h"
  20. #include "s5p_mfc_opr.h"
  21. #include "s5p_mfc_opr_v5.h"
  22. #include <asm/cacheflush.h>
  23. #include <linux/delay.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/err.h>
  26. #include <linux/firmware.h>
  27. #include <linux/io.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/mm.h>
  30. #include <linux/sched.h>
  31. #define OFFSETA(x) (((x) - dev->dma_base[BANK_L_CTX]) >> MFC_OFFSET_SHIFT)
  32. #define OFFSETB(x) (((x) - dev->dma_base[BANK_R_CTX]) >> MFC_OFFSET_SHIFT)
  33. /* Allocate temporary buffers for decoding */
  34. static int s5p_mfc_alloc_dec_temp_buffers_v5(struct s5p_mfc_ctx *ctx)
  35. {
  36. struct s5p_mfc_dev *dev = ctx->dev;
  37. struct s5p_mfc_buf_size_v5 *buf_size = dev->variant->buf_size->priv;
  38. int ret;
  39. ctx->dsc.size = buf_size->dsc;
  40. ret = s5p_mfc_alloc_priv_buf(dev, BANK_L_CTX, &ctx->dsc);
  41. if (ret) {
  42. mfc_err("Failed to allocate temporary buffer\n");
  43. return ret;
  44. }
  45. BUG_ON(ctx->dsc.dma & ((1 << MFC_BANK1_ALIGN_ORDER) - 1));
  46. memset(ctx->dsc.virt, 0, ctx->dsc.size);
  47. wmb();
  48. return 0;
  49. }
  50. /* Release temporary buffers for decoding */
  51. static void s5p_mfc_release_dec_desc_buffer_v5(struct s5p_mfc_ctx *ctx)
  52. {
  53. s5p_mfc_release_priv_buf(ctx->dev, &ctx->dsc);
  54. }
  55. /* Allocate codec buffers */
  56. static int s5p_mfc_alloc_codec_buffers_v5(struct s5p_mfc_ctx *ctx)
  57. {
  58. struct s5p_mfc_dev *dev = ctx->dev;
  59. unsigned int enc_ref_y_size = 0;
  60. unsigned int enc_ref_c_size = 0;
  61. unsigned int guard_width, guard_height;
  62. int ret;
  63. if (ctx->type == MFCINST_DECODER) {
  64. mfc_debug(2, "Luma size:%d Chroma size:%d MV size:%d\n",
  65. ctx->luma_size, ctx->chroma_size, ctx->mv_size);
  66. mfc_debug(2, "Totals bufs: %d\n", ctx->total_dpb_count);
  67. } else if (ctx->type == MFCINST_ENCODER) {
  68. enc_ref_y_size = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN)
  69. * ALIGN(ctx->img_height, S5P_FIMV_NV12MT_VALIGN);
  70. enc_ref_y_size = ALIGN(enc_ref_y_size, S5P_FIMV_NV12MT_SALIGN);
  71. if (ctx->codec_mode == S5P_MFC_CODEC_H264_ENC) {
  72. enc_ref_c_size = ALIGN(ctx->img_width,
  73. S5P_FIMV_NV12MT_HALIGN)
  74. * ALIGN(ctx->img_height >> 1,
  75. S5P_FIMV_NV12MT_VALIGN);
  76. enc_ref_c_size = ALIGN(enc_ref_c_size,
  77. S5P_FIMV_NV12MT_SALIGN);
  78. } else {
  79. guard_width = ALIGN(ctx->img_width + 16,
  80. S5P_FIMV_NV12MT_HALIGN);
  81. guard_height = ALIGN((ctx->img_height >> 1) + 4,
  82. S5P_FIMV_NV12MT_VALIGN);
  83. enc_ref_c_size = ALIGN(guard_width * guard_height,
  84. S5P_FIMV_NV12MT_SALIGN);
  85. }
  86. mfc_debug(2, "recon luma size: %d chroma size: %d\n",
  87. enc_ref_y_size, enc_ref_c_size);
  88. } else {
  89. return -EINVAL;
  90. }
  91. /* Codecs have different memory requirements */
  92. switch (ctx->codec_mode) {
  93. case S5P_MFC_CODEC_H264_DEC:
  94. ctx->bank1.size =
  95. ALIGN(S5P_FIMV_DEC_NB_IP_SIZE +
  96. S5P_FIMV_DEC_VERT_NB_MV_SIZE,
  97. S5P_FIMV_DEC_BUF_ALIGN);
  98. ctx->bank2.size = ctx->total_dpb_count * ctx->mv_size;
  99. break;
  100. case S5P_MFC_CODEC_MPEG4_DEC:
  101. ctx->bank1.size =
  102. ALIGN(S5P_FIMV_DEC_NB_DCAC_SIZE +
  103. S5P_FIMV_DEC_UPNB_MV_SIZE +
  104. S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE +
  105. S5P_FIMV_DEC_STX_PARSER_SIZE +
  106. S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE,
  107. S5P_FIMV_DEC_BUF_ALIGN);
  108. ctx->bank2.size = 0;
  109. break;
  110. case S5P_MFC_CODEC_VC1RCV_DEC:
  111. case S5P_MFC_CODEC_VC1_DEC:
  112. ctx->bank1.size =
  113. ALIGN(S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE +
  114. S5P_FIMV_DEC_UPNB_MV_SIZE +
  115. S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE +
  116. S5P_FIMV_DEC_NB_DCAC_SIZE +
  117. 3 * S5P_FIMV_DEC_VC1_BITPLANE_SIZE,
  118. S5P_FIMV_DEC_BUF_ALIGN);
  119. ctx->bank2.size = 0;
  120. break;
  121. case S5P_MFC_CODEC_MPEG2_DEC:
  122. ctx->bank1.size = 0;
  123. ctx->bank2.size = 0;
  124. break;
  125. case S5P_MFC_CODEC_H263_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. S5P_FIMV_DEC_BUF_ALIGN);
  132. ctx->bank2.size = 0;
  133. break;
  134. case S5P_MFC_CODEC_H264_ENC:
  135. ctx->bank1.size = (enc_ref_y_size * 2) +
  136. S5P_FIMV_ENC_UPMV_SIZE +
  137. S5P_FIMV_ENC_COLFLG_SIZE +
  138. S5P_FIMV_ENC_INTRAMD_SIZE +
  139. S5P_FIMV_ENC_NBORINFO_SIZE;
  140. ctx->bank2.size = (enc_ref_y_size * 2) +
  141. (enc_ref_c_size * 4) +
  142. S5P_FIMV_ENC_INTRAPRED_SIZE;
  143. break;
  144. case S5P_MFC_CODEC_MPEG4_ENC:
  145. ctx->bank1.size = (enc_ref_y_size * 2) +
  146. S5P_FIMV_ENC_UPMV_SIZE +
  147. S5P_FIMV_ENC_COLFLG_SIZE +
  148. S5P_FIMV_ENC_ACDCCOEF_SIZE;
  149. ctx->bank2.size = (enc_ref_y_size * 2) +
  150. (enc_ref_c_size * 4);
  151. break;
  152. case S5P_MFC_CODEC_H263_ENC:
  153. ctx->bank1.size = (enc_ref_y_size * 2) +
  154. S5P_FIMV_ENC_UPMV_SIZE +
  155. S5P_FIMV_ENC_ACDCCOEF_SIZE;
  156. ctx->bank2.size = (enc_ref_y_size * 2) +
  157. (enc_ref_c_size * 4);
  158. break;
  159. default:
  160. break;
  161. }
  162. /* Allocate only if memory from bank 1 is necessary */
  163. if (ctx->bank1.size > 0) {
  164. ret = s5p_mfc_alloc_priv_buf(dev, BANK_L_CTX, &ctx->bank1);
  165. if (ret) {
  166. mfc_err("Failed to allocate Bank1 temporary buffer\n");
  167. return ret;
  168. }
  169. BUG_ON(ctx->bank1.dma & ((1 << MFC_BANK1_ALIGN_ORDER) - 1));
  170. }
  171. /* Allocate only if memory from bank 2 is necessary */
  172. if (ctx->bank2.size > 0) {
  173. ret = s5p_mfc_alloc_priv_buf(dev, BANK_R_CTX, &ctx->bank2);
  174. if (ret) {
  175. mfc_err("Failed to allocate Bank2 temporary buffer\n");
  176. s5p_mfc_release_priv_buf(ctx->dev, &ctx->bank1);
  177. return ret;
  178. }
  179. BUG_ON(ctx->bank2.dma & ((1 << MFC_BANK2_ALIGN_ORDER) - 1));
  180. }
  181. return 0;
  182. }
  183. /* Release buffers allocated for codec */
  184. static void s5p_mfc_release_codec_buffers_v5(struct s5p_mfc_ctx *ctx)
  185. {
  186. s5p_mfc_release_priv_buf(ctx->dev, &ctx->bank1);
  187. s5p_mfc_release_priv_buf(ctx->dev, &ctx->bank2);
  188. }
  189. /* Allocate memory for instance data buffer */
  190. static int s5p_mfc_alloc_instance_buffer_v5(struct s5p_mfc_ctx *ctx)
  191. {
  192. struct s5p_mfc_dev *dev = ctx->dev;
  193. struct s5p_mfc_buf_size_v5 *buf_size = dev->variant->buf_size->priv;
  194. int ret;
  195. if (ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
  196. ctx->codec_mode == S5P_MFC_CODEC_H264_ENC)
  197. ctx->ctx.size = buf_size->h264_ctx;
  198. else
  199. ctx->ctx.size = buf_size->non_h264_ctx;
  200. ret = s5p_mfc_alloc_priv_buf(dev, BANK_L_CTX, &ctx->ctx);
  201. if (ret) {
  202. mfc_err("Failed to allocate instance buffer\n");
  203. return ret;
  204. }
  205. ctx->ctx.ofs = OFFSETA(ctx->ctx.dma);
  206. /* Zero content of the allocated memory */
  207. memset(ctx->ctx.virt, 0, ctx->ctx.size);
  208. wmb();
  209. /* Initialize shared memory */
  210. ctx->shm.size = buf_size->shm;
  211. ret = s5p_mfc_alloc_priv_buf(dev, BANK_L_CTX, &ctx->shm);
  212. if (ret) {
  213. mfc_err("Failed to allocate shared memory buffer\n");
  214. s5p_mfc_release_priv_buf(dev, &ctx->ctx);
  215. return ret;
  216. }
  217. /* shared memory offset only keeps the offset from base (port a) */
  218. ctx->shm.ofs = ctx->shm.dma - dev->dma_base[BANK_L_CTX];
  219. BUG_ON(ctx->shm.ofs & ((1 << MFC_BANK1_ALIGN_ORDER) - 1));
  220. memset(ctx->shm.virt, 0, buf_size->shm);
  221. wmb();
  222. return 0;
  223. }
  224. /* Release instance buffer */
  225. static void s5p_mfc_release_instance_buffer_v5(struct s5p_mfc_ctx *ctx)
  226. {
  227. s5p_mfc_release_priv_buf(ctx->dev, &ctx->ctx);
  228. s5p_mfc_release_priv_buf(ctx->dev, &ctx->shm);
  229. }
  230. static int s5p_mfc_alloc_dev_context_buffer_v5(struct s5p_mfc_dev *dev)
  231. {
  232. /* NOP */
  233. return 0;
  234. }
  235. static void s5p_mfc_release_dev_context_buffer_v5(struct s5p_mfc_dev *dev)
  236. {
  237. /* NOP */
  238. }
  239. static void s5p_mfc_write_info_v5(struct s5p_mfc_ctx *ctx, unsigned int data,
  240. unsigned int ofs)
  241. {
  242. *(u32 *)(ctx->shm.virt + ofs) = data;
  243. wmb();
  244. }
  245. static unsigned int s5p_mfc_read_info_v5(struct s5p_mfc_ctx *ctx,
  246. unsigned long ofs)
  247. {
  248. rmb();
  249. return *(u32 *)(ctx->shm.virt + ofs);
  250. }
  251. static void s5p_mfc_dec_calc_dpb_size_v5(struct s5p_mfc_ctx *ctx)
  252. {
  253. unsigned int guard_width, guard_height;
  254. ctx->buf_width = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN);
  255. ctx->buf_height = ALIGN(ctx->img_height, S5P_FIMV_NV12MT_VALIGN);
  256. mfc_debug(2,
  257. "SEQ Done: Movie dimensions %dx%d, buffer dimensions: %dx%d\n",
  258. ctx->img_width, ctx->img_height, ctx->buf_width,
  259. ctx->buf_height);
  260. if (ctx->codec_mode == S5P_MFC_CODEC_H264_DEC) {
  261. ctx->luma_size = ALIGN(ctx->buf_width * ctx->buf_height,
  262. S5P_FIMV_DEC_BUF_ALIGN);
  263. ctx->chroma_size = ALIGN(ctx->buf_width *
  264. ALIGN((ctx->img_height >> 1),
  265. S5P_FIMV_NV12MT_VALIGN),
  266. S5P_FIMV_DEC_BUF_ALIGN);
  267. ctx->mv_size = ALIGN(ctx->buf_width *
  268. ALIGN((ctx->buf_height >> 2),
  269. S5P_FIMV_NV12MT_VALIGN),
  270. S5P_FIMV_DEC_BUF_ALIGN);
  271. } else {
  272. guard_width =
  273. ALIGN(ctx->img_width + 24, S5P_FIMV_NV12MT_HALIGN);
  274. guard_height =
  275. ALIGN(ctx->img_height + 16, S5P_FIMV_NV12MT_VALIGN);
  276. ctx->luma_size = ALIGN(guard_width * guard_height,
  277. S5P_FIMV_DEC_BUF_ALIGN);
  278. guard_width =
  279. ALIGN(ctx->img_width + 16, S5P_FIMV_NV12MT_HALIGN);
  280. guard_height =
  281. ALIGN((ctx->img_height >> 1) + 4,
  282. S5P_FIMV_NV12MT_VALIGN);
  283. ctx->chroma_size = ALIGN(guard_width * guard_height,
  284. S5P_FIMV_DEC_BUF_ALIGN);
  285. ctx->mv_size = 0;
  286. }
  287. }
  288. static void s5p_mfc_enc_calc_src_size_v5(struct s5p_mfc_ctx *ctx)
  289. {
  290. if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12M) {
  291. ctx->buf_width = ALIGN(ctx->img_width, S5P_FIMV_NV12M_HALIGN);
  292. ctx->luma_size = ALIGN(ctx->img_width, S5P_FIMV_NV12M_HALIGN)
  293. * ALIGN(ctx->img_height, S5P_FIMV_NV12M_LVALIGN);
  294. ctx->chroma_size = ALIGN(ctx->img_width, S5P_FIMV_NV12M_HALIGN)
  295. * ALIGN((ctx->img_height >> 1), S5P_FIMV_NV12M_CVALIGN);
  296. ctx->luma_size = ALIGN(ctx->luma_size, S5P_FIMV_NV12M_SALIGN);
  297. ctx->chroma_size =
  298. ALIGN(ctx->chroma_size, S5P_FIMV_NV12M_SALIGN);
  299. } else if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12MT) {
  300. ctx->buf_width = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN);
  301. ctx->luma_size = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN)
  302. * ALIGN(ctx->img_height, S5P_FIMV_NV12MT_VALIGN);
  303. ctx->chroma_size =
  304. ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN)
  305. * ALIGN((ctx->img_height >> 1), S5P_FIMV_NV12MT_VALIGN);
  306. ctx->luma_size = ALIGN(ctx->luma_size, S5P_FIMV_NV12MT_SALIGN);
  307. ctx->chroma_size =
  308. ALIGN(ctx->chroma_size, S5P_FIMV_NV12MT_SALIGN);
  309. }
  310. }
  311. /* Set registers for decoding temporary buffers */
  312. static void s5p_mfc_set_dec_desc_buffer(struct s5p_mfc_ctx *ctx)
  313. {
  314. struct s5p_mfc_dev *dev = ctx->dev;
  315. struct s5p_mfc_buf_size_v5 *buf_size = dev->variant->buf_size->priv;
  316. mfc_write(dev, OFFSETA(ctx->dsc.dma), S5P_FIMV_SI_CH0_DESC_ADR);
  317. mfc_write(dev, buf_size->dsc, S5P_FIMV_SI_CH0_DESC_SIZE);
  318. }
  319. /* Set registers for shared buffer */
  320. static void s5p_mfc_set_shared_buffer(struct s5p_mfc_ctx *ctx)
  321. {
  322. struct s5p_mfc_dev *dev = ctx->dev;
  323. mfc_write(dev, ctx->shm.ofs, S5P_FIMV_SI_CH0_HOST_WR_ADR);
  324. }
  325. /* Set registers for decoding stream buffer */
  326. static int s5p_mfc_set_dec_stream_buffer_v5(struct s5p_mfc_ctx *ctx,
  327. int buf_addr, unsigned int start_num_byte,
  328. unsigned int buf_size)
  329. {
  330. struct s5p_mfc_dev *dev = ctx->dev;
  331. mfc_write(dev, OFFSETA(buf_addr), S5P_FIMV_SI_CH0_SB_ST_ADR);
  332. mfc_write(dev, ctx->dec_src_buf_size, S5P_FIMV_SI_CH0_CPB_SIZE);
  333. mfc_write(dev, buf_size, S5P_FIMV_SI_CH0_SB_FRM_SIZE);
  334. s5p_mfc_write_info_v5(ctx, start_num_byte, START_BYTE_NUM);
  335. return 0;
  336. }
  337. /* Set decoding frame buffer */
  338. static int s5p_mfc_set_dec_frame_buffer_v5(struct s5p_mfc_ctx *ctx)
  339. {
  340. unsigned int frame_size_lu, i;
  341. unsigned int frame_size_ch, frame_size_mv;
  342. struct s5p_mfc_dev *dev = ctx->dev;
  343. unsigned int dpb;
  344. size_t buf_addr1, buf_addr2;
  345. int buf_size1, buf_size2;
  346. buf_addr1 = ctx->bank1.dma;
  347. buf_size1 = ctx->bank1.size;
  348. buf_addr2 = ctx->bank2.dma;
  349. buf_size2 = ctx->bank2.size;
  350. dpb = mfc_read(dev, S5P_FIMV_SI_CH0_DPB_CONF_CTRL) &
  351. ~S5P_FIMV_DPB_COUNT_MASK;
  352. mfc_write(dev, ctx->total_dpb_count | dpb,
  353. S5P_FIMV_SI_CH0_DPB_CONF_CTRL);
  354. s5p_mfc_set_shared_buffer(ctx);
  355. switch (ctx->codec_mode) {
  356. case S5P_MFC_CODEC_H264_DEC:
  357. mfc_write(dev, OFFSETA(buf_addr1),
  358. S5P_FIMV_H264_VERT_NB_MV_ADR);
  359. buf_addr1 += S5P_FIMV_DEC_VERT_NB_MV_SIZE;
  360. buf_size1 -= S5P_FIMV_DEC_VERT_NB_MV_SIZE;
  361. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H264_NB_IP_ADR);
  362. buf_addr1 += S5P_FIMV_DEC_NB_IP_SIZE;
  363. buf_size1 -= S5P_FIMV_DEC_NB_IP_SIZE;
  364. break;
  365. case S5P_MFC_CODEC_MPEG4_DEC:
  366. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_MPEG4_NB_DCAC_ADR);
  367. buf_addr1 += S5P_FIMV_DEC_NB_DCAC_SIZE;
  368. buf_size1 -= S5P_FIMV_DEC_NB_DCAC_SIZE;
  369. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_MPEG4_UP_NB_MV_ADR);
  370. buf_addr1 += S5P_FIMV_DEC_UPNB_MV_SIZE;
  371. buf_size1 -= S5P_FIMV_DEC_UPNB_MV_SIZE;
  372. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_MPEG4_SA_MV_ADR);
  373. buf_addr1 += S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE;
  374. buf_size1 -= S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE;
  375. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_MPEG4_SP_ADR);
  376. buf_addr1 += S5P_FIMV_DEC_STX_PARSER_SIZE;
  377. buf_size1 -= S5P_FIMV_DEC_STX_PARSER_SIZE;
  378. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_MPEG4_OT_LINE_ADR);
  379. buf_addr1 += S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE;
  380. buf_size1 -= S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE;
  381. break;
  382. case S5P_MFC_CODEC_H263_DEC:
  383. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H263_OT_LINE_ADR);
  384. buf_addr1 += S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE;
  385. buf_size1 -= S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE;
  386. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H263_UP_NB_MV_ADR);
  387. buf_addr1 += S5P_FIMV_DEC_UPNB_MV_SIZE;
  388. buf_size1 -= S5P_FIMV_DEC_UPNB_MV_SIZE;
  389. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H263_SA_MV_ADR);
  390. buf_addr1 += S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE;
  391. buf_size1 -= S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE;
  392. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H263_NB_DCAC_ADR);
  393. buf_addr1 += S5P_FIMV_DEC_NB_DCAC_SIZE;
  394. buf_size1 -= S5P_FIMV_DEC_NB_DCAC_SIZE;
  395. break;
  396. case S5P_MFC_CODEC_VC1_DEC:
  397. case S5P_MFC_CODEC_VC1RCV_DEC:
  398. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_NB_DCAC_ADR);
  399. buf_addr1 += S5P_FIMV_DEC_NB_DCAC_SIZE;
  400. buf_size1 -= S5P_FIMV_DEC_NB_DCAC_SIZE;
  401. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_OT_LINE_ADR);
  402. buf_addr1 += S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE;
  403. buf_size1 -= S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE;
  404. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_UP_NB_MV_ADR);
  405. buf_addr1 += S5P_FIMV_DEC_UPNB_MV_SIZE;
  406. buf_size1 -= S5P_FIMV_DEC_UPNB_MV_SIZE;
  407. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_SA_MV_ADR);
  408. buf_addr1 += S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE;
  409. buf_size1 -= S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE;
  410. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_BITPLANE3_ADR);
  411. buf_addr1 += S5P_FIMV_DEC_VC1_BITPLANE_SIZE;
  412. buf_size1 -= S5P_FIMV_DEC_VC1_BITPLANE_SIZE;
  413. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_BITPLANE2_ADR);
  414. buf_addr1 += S5P_FIMV_DEC_VC1_BITPLANE_SIZE;
  415. buf_size1 -= S5P_FIMV_DEC_VC1_BITPLANE_SIZE;
  416. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_BITPLANE1_ADR);
  417. buf_addr1 += S5P_FIMV_DEC_VC1_BITPLANE_SIZE;
  418. buf_size1 -= S5P_FIMV_DEC_VC1_BITPLANE_SIZE;
  419. break;
  420. case S5P_MFC_CODEC_MPEG2_DEC:
  421. break;
  422. default:
  423. mfc_err("Unknown codec for decoding (%x)\n",
  424. ctx->codec_mode);
  425. return -EINVAL;
  426. }
  427. frame_size_lu = ctx->luma_size;
  428. frame_size_ch = ctx->chroma_size;
  429. frame_size_mv = ctx->mv_size;
  430. mfc_debug(2, "Frm size: %d ch: %d mv: %d\n", frame_size_lu, frame_size_ch,
  431. frame_size_mv);
  432. for (i = 0; i < ctx->total_dpb_count; i++) {
  433. /* Bank2 */
  434. mfc_debug(2, "Luma %d: %zx\n", i,
  435. ctx->dst_bufs[i].cookie.raw.luma);
  436. mfc_write(dev, OFFSETB(ctx->dst_bufs[i].cookie.raw.luma),
  437. S5P_FIMV_DEC_LUMA_ADR + i * 4);
  438. mfc_debug(2, "\tChroma %d: %zx\n", i,
  439. ctx->dst_bufs[i].cookie.raw.chroma);
  440. mfc_write(dev, OFFSETA(ctx->dst_bufs[i].cookie.raw.chroma),
  441. S5P_FIMV_DEC_CHROMA_ADR + i * 4);
  442. if (ctx->codec_mode == S5P_MFC_CODEC_H264_DEC) {
  443. mfc_debug(2, "\tBuf2: %zx, size: %d\n",
  444. buf_addr2, buf_size2);
  445. mfc_write(dev, OFFSETB(buf_addr2),
  446. S5P_FIMV_H264_MV_ADR + i * 4);
  447. buf_addr2 += frame_size_mv;
  448. buf_size2 -= frame_size_mv;
  449. }
  450. }
  451. mfc_debug(2, "Buf1: %zu, buf_size1: %d\n", buf_addr1, buf_size1);
  452. mfc_debug(2, "Buf 1/2 size after: %d/%d (frames %d)\n",
  453. buf_size1, buf_size2, ctx->total_dpb_count);
  454. if (buf_size1 < 0 || buf_size2 < 0) {
  455. mfc_debug(2, "Not enough memory has been allocated\n");
  456. return -ENOMEM;
  457. }
  458. s5p_mfc_write_info_v5(ctx, frame_size_lu, ALLOC_LUMA_DPB_SIZE);
  459. s5p_mfc_write_info_v5(ctx, frame_size_ch, ALLOC_CHROMA_DPB_SIZE);
  460. if (ctx->codec_mode == S5P_MFC_CODEC_H264_DEC)
  461. s5p_mfc_write_info_v5(ctx, frame_size_mv, ALLOC_MV_SIZE);
  462. mfc_write(dev, ((S5P_FIMV_CH_INIT_BUFS & S5P_FIMV_CH_MASK)
  463. << S5P_FIMV_CH_SHIFT) | (ctx->inst_no),
  464. S5P_FIMV_SI_CH0_INST_ID);
  465. return 0;
  466. }
  467. /* Set registers for encoding stream buffer */
  468. static int s5p_mfc_set_enc_stream_buffer_v5(struct s5p_mfc_ctx *ctx,
  469. unsigned long addr, unsigned int size)
  470. {
  471. struct s5p_mfc_dev *dev = ctx->dev;
  472. mfc_write(dev, OFFSETA(addr), S5P_FIMV_ENC_SI_CH0_SB_ADR);
  473. mfc_write(dev, size, S5P_FIMV_ENC_SI_CH0_SB_SIZE);
  474. return 0;
  475. }
  476. static void s5p_mfc_set_enc_frame_buffer_v5(struct s5p_mfc_ctx *ctx,
  477. unsigned long y_addr, unsigned long c_addr)
  478. {
  479. struct s5p_mfc_dev *dev = ctx->dev;
  480. mfc_write(dev, OFFSETB(y_addr), S5P_FIMV_ENC_SI_CH0_CUR_Y_ADR);
  481. mfc_write(dev, OFFSETB(c_addr), S5P_FIMV_ENC_SI_CH0_CUR_C_ADR);
  482. }
  483. static void s5p_mfc_get_enc_frame_buffer_v5(struct s5p_mfc_ctx *ctx,
  484. unsigned long *y_addr, unsigned long *c_addr)
  485. {
  486. struct s5p_mfc_dev *dev = ctx->dev;
  487. *y_addr = dev->dma_base[BANK_R_CTX] +
  488. (mfc_read(dev, S5P_FIMV_ENCODED_Y_ADDR) << MFC_OFFSET_SHIFT);
  489. *c_addr = dev->dma_base[BANK_R_CTX] +
  490. (mfc_read(dev, S5P_FIMV_ENCODED_C_ADDR) << MFC_OFFSET_SHIFT);
  491. }
  492. /* Set encoding ref & codec buffer */
  493. static int s5p_mfc_set_enc_ref_buffer_v5(struct s5p_mfc_ctx *ctx)
  494. {
  495. struct s5p_mfc_dev *dev = ctx->dev;
  496. size_t buf_addr1, buf_addr2;
  497. size_t buf_size1, buf_size2;
  498. unsigned int enc_ref_y_size, enc_ref_c_size;
  499. unsigned int guard_width, guard_height;
  500. int i;
  501. buf_addr1 = ctx->bank1.dma;
  502. buf_size1 = ctx->bank1.size;
  503. buf_addr2 = ctx->bank2.dma;
  504. buf_size2 = ctx->bank2.size;
  505. enc_ref_y_size = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN)
  506. * ALIGN(ctx->img_height, S5P_FIMV_NV12MT_VALIGN);
  507. enc_ref_y_size = ALIGN(enc_ref_y_size, S5P_FIMV_NV12MT_SALIGN);
  508. if (ctx->codec_mode == S5P_MFC_CODEC_H264_ENC) {
  509. enc_ref_c_size = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN)
  510. * ALIGN((ctx->img_height >> 1), S5P_FIMV_NV12MT_VALIGN);
  511. enc_ref_c_size = ALIGN(enc_ref_c_size, S5P_FIMV_NV12MT_SALIGN);
  512. } else {
  513. guard_width = ALIGN(ctx->img_width + 16,
  514. S5P_FIMV_NV12MT_HALIGN);
  515. guard_height = ALIGN((ctx->img_height >> 1) + 4,
  516. S5P_FIMV_NV12MT_VALIGN);
  517. enc_ref_c_size = ALIGN(guard_width * guard_height,
  518. S5P_FIMV_NV12MT_SALIGN);
  519. }
  520. mfc_debug(2, "buf_size1: %zu, buf_size2: %zu\n", buf_size1, buf_size2);
  521. switch (ctx->codec_mode) {
  522. case S5P_MFC_CODEC_H264_ENC:
  523. for (i = 0; i < 2; i++) {
  524. mfc_write(dev, OFFSETA(buf_addr1),
  525. S5P_FIMV_ENC_REF0_LUMA_ADR + (4 * i));
  526. buf_addr1 += enc_ref_y_size;
  527. buf_size1 -= enc_ref_y_size;
  528. mfc_write(dev, OFFSETB(buf_addr2),
  529. S5P_FIMV_ENC_REF2_LUMA_ADR + (4 * i));
  530. buf_addr2 += enc_ref_y_size;
  531. buf_size2 -= enc_ref_y_size;
  532. }
  533. for (i = 0; i < 4; i++) {
  534. mfc_write(dev, OFFSETB(buf_addr2),
  535. S5P_FIMV_ENC_REF0_CHROMA_ADR + (4 * i));
  536. buf_addr2 += enc_ref_c_size;
  537. buf_size2 -= enc_ref_c_size;
  538. }
  539. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H264_UP_MV_ADR);
  540. buf_addr1 += S5P_FIMV_ENC_UPMV_SIZE;
  541. buf_size1 -= S5P_FIMV_ENC_UPMV_SIZE;
  542. mfc_write(dev, OFFSETA(buf_addr1),
  543. S5P_FIMV_H264_COZERO_FLAG_ADR);
  544. buf_addr1 += S5P_FIMV_ENC_COLFLG_SIZE;
  545. buf_size1 -= S5P_FIMV_ENC_COLFLG_SIZE;
  546. mfc_write(dev, OFFSETA(buf_addr1),
  547. S5P_FIMV_H264_UP_INTRA_MD_ADR);
  548. buf_addr1 += S5P_FIMV_ENC_INTRAMD_SIZE;
  549. buf_size1 -= S5P_FIMV_ENC_INTRAMD_SIZE;
  550. mfc_write(dev, OFFSETB(buf_addr2),
  551. S5P_FIMV_H264_UP_INTRA_PRED_ADR);
  552. buf_addr2 += S5P_FIMV_ENC_INTRAPRED_SIZE;
  553. buf_size2 -= S5P_FIMV_ENC_INTRAPRED_SIZE;
  554. mfc_write(dev, OFFSETA(buf_addr1),
  555. S5P_FIMV_H264_NBOR_INFO_ADR);
  556. buf_addr1 += S5P_FIMV_ENC_NBORINFO_SIZE;
  557. buf_size1 -= S5P_FIMV_ENC_NBORINFO_SIZE;
  558. mfc_debug(2, "buf_size1: %zu, buf_size2: %zu\n",
  559. buf_size1, buf_size2);
  560. break;
  561. case S5P_MFC_CODEC_MPEG4_ENC:
  562. for (i = 0; i < 2; i++) {
  563. mfc_write(dev, OFFSETA(buf_addr1),
  564. S5P_FIMV_ENC_REF0_LUMA_ADR + (4 * i));
  565. buf_addr1 += enc_ref_y_size;
  566. buf_size1 -= enc_ref_y_size;
  567. mfc_write(dev, OFFSETB(buf_addr2),
  568. S5P_FIMV_ENC_REF2_LUMA_ADR + (4 * i));
  569. buf_addr2 += enc_ref_y_size;
  570. buf_size2 -= enc_ref_y_size;
  571. }
  572. for (i = 0; i < 4; i++) {
  573. mfc_write(dev, OFFSETB(buf_addr2),
  574. S5P_FIMV_ENC_REF0_CHROMA_ADR + (4 * i));
  575. buf_addr2 += enc_ref_c_size;
  576. buf_size2 -= enc_ref_c_size;
  577. }
  578. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_MPEG4_UP_MV_ADR);
  579. buf_addr1 += S5P_FIMV_ENC_UPMV_SIZE;
  580. buf_size1 -= S5P_FIMV_ENC_UPMV_SIZE;
  581. mfc_write(dev, OFFSETA(buf_addr1),
  582. S5P_FIMV_MPEG4_COZERO_FLAG_ADR);
  583. buf_addr1 += S5P_FIMV_ENC_COLFLG_SIZE;
  584. buf_size1 -= S5P_FIMV_ENC_COLFLG_SIZE;
  585. mfc_write(dev, OFFSETA(buf_addr1),
  586. S5P_FIMV_MPEG4_ACDC_COEF_ADR);
  587. buf_addr1 += S5P_FIMV_ENC_ACDCCOEF_SIZE;
  588. buf_size1 -= S5P_FIMV_ENC_ACDCCOEF_SIZE;
  589. mfc_debug(2, "buf_size1: %zu, buf_size2: %zu\n",
  590. buf_size1, buf_size2);
  591. break;
  592. case S5P_MFC_CODEC_H263_ENC:
  593. for (i = 0; i < 2; i++) {
  594. mfc_write(dev, OFFSETA(buf_addr1),
  595. S5P_FIMV_ENC_REF0_LUMA_ADR + (4 * i));
  596. buf_addr1 += enc_ref_y_size;
  597. buf_size1 -= enc_ref_y_size;
  598. mfc_write(dev, OFFSETB(buf_addr2),
  599. S5P_FIMV_ENC_REF2_LUMA_ADR + (4 * i));
  600. buf_addr2 += enc_ref_y_size;
  601. buf_size2 -= enc_ref_y_size;
  602. }
  603. for (i = 0; i < 4; i++) {
  604. mfc_write(dev, OFFSETB(buf_addr2),
  605. S5P_FIMV_ENC_REF0_CHROMA_ADR + (4 * i));
  606. buf_addr2 += enc_ref_c_size;
  607. buf_size2 -= enc_ref_c_size;
  608. }
  609. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H263_UP_MV_ADR);
  610. buf_addr1 += S5P_FIMV_ENC_UPMV_SIZE;
  611. buf_size1 -= S5P_FIMV_ENC_UPMV_SIZE;
  612. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H263_ACDC_COEF_ADR);
  613. buf_addr1 += S5P_FIMV_ENC_ACDCCOEF_SIZE;
  614. buf_size1 -= S5P_FIMV_ENC_ACDCCOEF_SIZE;
  615. mfc_debug(2, "buf_size1: %zu, buf_size2: %zu\n",
  616. buf_size1, buf_size2);
  617. break;
  618. default:
  619. mfc_err("Unknown codec set for encoding: %d\n",
  620. ctx->codec_mode);
  621. return -EINVAL;
  622. }
  623. return 0;
  624. }
  625. static int s5p_mfc_set_enc_params(struct s5p_mfc_ctx *ctx)
  626. {
  627. struct s5p_mfc_dev *dev = ctx->dev;
  628. struct s5p_mfc_enc_params *p = &ctx->enc_params;
  629. unsigned int reg;
  630. unsigned int shm;
  631. /* width */
  632. mfc_write(dev, ctx->img_width, S5P_FIMV_ENC_HSIZE_PX);
  633. /* height */
  634. mfc_write(dev, ctx->img_height, S5P_FIMV_ENC_VSIZE_PX);
  635. /* pictype : enable, IDR period */
  636. reg = mfc_read(dev, S5P_FIMV_ENC_PIC_TYPE_CTRL);
  637. reg |= (1 << 18);
  638. reg &= ~(0xFFFF);
  639. reg |= p->gop_size;
  640. mfc_write(dev, reg, S5P_FIMV_ENC_PIC_TYPE_CTRL);
  641. mfc_write(dev, 0, S5P_FIMV_ENC_B_RECON_WRITE_ON);
  642. /* multi-slice control */
  643. /* multi-slice MB number or bit size */
  644. mfc_write(dev, p->slice_mode, S5P_FIMV_ENC_MSLICE_CTRL);
  645. if (p->slice_mode == V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB) {
  646. mfc_write(dev, p->slice_mb, S5P_FIMV_ENC_MSLICE_MB);
  647. } else if (p->slice_mode == V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES) {
  648. mfc_write(dev, p->slice_bit, S5P_FIMV_ENC_MSLICE_BIT);
  649. } else {
  650. mfc_write(dev, 0, S5P_FIMV_ENC_MSLICE_MB);
  651. mfc_write(dev, 0, S5P_FIMV_ENC_MSLICE_BIT);
  652. }
  653. /* cyclic intra refresh */
  654. mfc_write(dev, p->intra_refresh_mb, S5P_FIMV_ENC_CIR_CTRL);
  655. /* memory structure cur. frame */
  656. if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12M)
  657. mfc_write(dev, 0, S5P_FIMV_ENC_MAP_FOR_CUR);
  658. else if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12MT)
  659. mfc_write(dev, 3, S5P_FIMV_ENC_MAP_FOR_CUR);
  660. /* padding control & value */
  661. reg = mfc_read(dev, S5P_FIMV_ENC_PADDING_CTRL);
  662. if (p->pad) {
  663. /** enable */
  664. reg |= (1 << 31);
  665. /** cr value */
  666. reg &= ~(0xFF << 16);
  667. reg |= (p->pad_cr << 16);
  668. /** cb value */
  669. reg &= ~(0xFF << 8);
  670. reg |= (p->pad_cb << 8);
  671. /** y value */
  672. reg &= ~(0xFF);
  673. reg |= (p->pad_luma);
  674. } else {
  675. /** disable & all value clear */
  676. reg = 0;
  677. }
  678. mfc_write(dev, reg, S5P_FIMV_ENC_PADDING_CTRL);
  679. /* rate control config. */
  680. reg = mfc_read(dev, S5P_FIMV_ENC_RC_CONFIG);
  681. /** frame-level rate control */
  682. reg &= ~(0x1 << 9);
  683. reg |= (p->rc_frame << 9);
  684. mfc_write(dev, reg, S5P_FIMV_ENC_RC_CONFIG);
  685. /* bit rate */
  686. if (p->rc_frame)
  687. mfc_write(dev, p->rc_bitrate,
  688. S5P_FIMV_ENC_RC_BIT_RATE);
  689. else
  690. mfc_write(dev, 0, S5P_FIMV_ENC_RC_BIT_RATE);
  691. /* reaction coefficient */
  692. if (p->rc_frame)
  693. mfc_write(dev, p->rc_reaction_coeff, S5P_FIMV_ENC_RC_RPARA);
  694. shm = s5p_mfc_read_info_v5(ctx, EXT_ENC_CONTROL);
  695. /* seq header ctrl */
  696. shm &= ~(0x1 << 3);
  697. shm |= (p->seq_hdr_mode << 3);
  698. /* frame skip mode */
  699. shm &= ~(0x3 << 1);
  700. shm |= (p->frame_skip_mode << 1);
  701. s5p_mfc_write_info_v5(ctx, shm, EXT_ENC_CONTROL);
  702. /* fixed target bit */
  703. s5p_mfc_write_info_v5(ctx, p->fixed_target_bit, RC_CONTROL_CONFIG);
  704. return 0;
  705. }
  706. static int s5p_mfc_set_enc_params_h264(struct s5p_mfc_ctx *ctx)
  707. {
  708. struct s5p_mfc_dev *dev = ctx->dev;
  709. struct s5p_mfc_enc_params *p = &ctx->enc_params;
  710. struct s5p_mfc_h264_enc_params *p_264 = &p->codec.h264;
  711. unsigned int reg;
  712. unsigned int shm;
  713. s5p_mfc_set_enc_params(ctx);
  714. /* pictype : number of B */
  715. reg = mfc_read(dev, S5P_FIMV_ENC_PIC_TYPE_CTRL);
  716. /* num_b_frame - 0 ~ 2 */
  717. reg &= ~(0x3 << 16);
  718. reg |= (p->num_b_frame << 16);
  719. mfc_write(dev, reg, S5P_FIMV_ENC_PIC_TYPE_CTRL);
  720. /* profile & level */
  721. reg = mfc_read(dev, S5P_FIMV_ENC_PROFILE);
  722. /* level */
  723. reg &= ~(0xFF << 8);
  724. reg |= (p_264->level << 8);
  725. /* profile - 0 ~ 2 */
  726. reg &= ~(0x3F);
  727. reg |= p_264->profile;
  728. mfc_write(dev, reg, S5P_FIMV_ENC_PROFILE);
  729. /* interlace */
  730. mfc_write(dev, p_264->interlace, S5P_FIMV_ENC_PIC_STRUCT);
  731. /* height */
  732. if (p_264->interlace)
  733. mfc_write(dev, ctx->img_height >> 1, S5P_FIMV_ENC_VSIZE_PX);
  734. /* loopfilter ctrl */
  735. mfc_write(dev, p_264->loop_filter_mode, S5P_FIMV_ENC_LF_CTRL);
  736. /* loopfilter alpha offset */
  737. if (p_264->loop_filter_alpha < 0) {
  738. reg = 0x10;
  739. reg |= (0xFF - p_264->loop_filter_alpha) + 1;
  740. } else {
  741. reg = 0x00;
  742. reg |= (p_264->loop_filter_alpha & 0xF);
  743. }
  744. mfc_write(dev, reg, S5P_FIMV_ENC_ALPHA_OFF);
  745. /* loopfilter beta offset */
  746. if (p_264->loop_filter_beta < 0) {
  747. reg = 0x10;
  748. reg |= (0xFF - p_264->loop_filter_beta) + 1;
  749. } else {
  750. reg = 0x00;
  751. reg |= (p_264->loop_filter_beta & 0xF);
  752. }
  753. mfc_write(dev, reg, S5P_FIMV_ENC_BETA_OFF);
  754. /* entropy coding mode */
  755. if (p_264->entropy_mode == V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC)
  756. mfc_write(dev, 1, S5P_FIMV_ENC_H264_ENTROPY_MODE);
  757. else
  758. mfc_write(dev, 0, S5P_FIMV_ENC_H264_ENTROPY_MODE);
  759. /* number of ref. picture */
  760. reg = mfc_read(dev, S5P_FIMV_ENC_H264_NUM_OF_REF);
  761. /* num of ref. pictures of P */
  762. reg &= ~(0x3 << 5);
  763. reg |= (p_264->num_ref_pic_4p << 5);
  764. /* max number of ref. pictures */
  765. reg &= ~(0x1F);
  766. reg |= p_264->max_ref_pic;
  767. mfc_write(dev, reg, S5P_FIMV_ENC_H264_NUM_OF_REF);
  768. /* 8x8 transform enable */
  769. mfc_write(dev, p_264->_8x8_transform, S5P_FIMV_ENC_H264_TRANS_FLAG);
  770. /* rate control config. */
  771. reg = mfc_read(dev, S5P_FIMV_ENC_RC_CONFIG);
  772. /* macroblock level rate control */
  773. reg &= ~(0x1 << 8);
  774. reg |= (p->rc_mb << 8);
  775. /* frame QP */
  776. reg &= ~(0x3F);
  777. reg |= p_264->rc_frame_qp;
  778. mfc_write(dev, reg, S5P_FIMV_ENC_RC_CONFIG);
  779. /* frame rate */
  780. if (p->rc_frame && p->rc_framerate_denom)
  781. mfc_write(dev, p->rc_framerate_num * 1000
  782. / p->rc_framerate_denom, S5P_FIMV_ENC_RC_FRAME_RATE);
  783. else
  784. mfc_write(dev, 0, S5P_FIMV_ENC_RC_FRAME_RATE);
  785. /* max & min value of QP */
  786. reg = mfc_read(dev, S5P_FIMV_ENC_RC_QBOUND);
  787. /* max QP */
  788. reg &= ~(0x3F << 8);
  789. reg |= (p_264->rc_max_qp << 8);
  790. /* min QP */
  791. reg &= ~(0x3F);
  792. reg |= p_264->rc_min_qp;
  793. mfc_write(dev, reg, S5P_FIMV_ENC_RC_QBOUND);
  794. /* macroblock adaptive scaling features */
  795. if (p->rc_mb) {
  796. reg = mfc_read(dev, S5P_FIMV_ENC_RC_MB_CTRL);
  797. /* dark region */
  798. reg &= ~(0x1 << 3);
  799. reg |= (p_264->rc_mb_dark << 3);
  800. /* smooth region */
  801. reg &= ~(0x1 << 2);
  802. reg |= (p_264->rc_mb_smooth << 2);
  803. /* static region */
  804. reg &= ~(0x1 << 1);
  805. reg |= (p_264->rc_mb_static << 1);
  806. /* high activity region */
  807. reg &= ~(0x1);
  808. reg |= p_264->rc_mb_activity;
  809. mfc_write(dev, reg, S5P_FIMV_ENC_RC_MB_CTRL);
  810. }
  811. if (!p->rc_frame && !p->rc_mb) {
  812. shm = s5p_mfc_read_info_v5(ctx, P_B_FRAME_QP);
  813. shm &= ~(0xFFF);
  814. shm |= ((p_264->rc_b_frame_qp & 0x3F) << 6);
  815. shm |= (p_264->rc_p_frame_qp & 0x3F);
  816. s5p_mfc_write_info_v5(ctx, shm, P_B_FRAME_QP);
  817. }
  818. /* extended encoder ctrl */
  819. shm = s5p_mfc_read_info_v5(ctx, EXT_ENC_CONTROL);
  820. /* AR VUI control */
  821. shm &= ~(0x1 << 15);
  822. shm |= (p_264->vui_sar << 1);
  823. s5p_mfc_write_info_v5(ctx, shm, EXT_ENC_CONTROL);
  824. if (p_264->vui_sar) {
  825. /* aspect ration IDC */
  826. shm = s5p_mfc_read_info_v5(ctx, SAMPLE_ASPECT_RATIO_IDC);
  827. shm &= ~(0xFF);
  828. shm |= p_264->vui_sar_idc;
  829. s5p_mfc_write_info_v5(ctx, shm, SAMPLE_ASPECT_RATIO_IDC);
  830. if (p_264->vui_sar_idc == 0xFF) {
  831. /* sample AR info */
  832. shm = s5p_mfc_read_info_v5(ctx, EXTENDED_SAR);
  833. shm &= ~(0xFFFFFFFF);
  834. shm |= p_264->vui_ext_sar_width << 16;
  835. shm |= p_264->vui_ext_sar_height;
  836. s5p_mfc_write_info_v5(ctx, shm, EXTENDED_SAR);
  837. }
  838. }
  839. /* intra picture period for H.264 */
  840. shm = s5p_mfc_read_info_v5(ctx, H264_I_PERIOD);
  841. /* control */
  842. shm &= ~(0x1 << 16);
  843. shm |= (p_264->open_gop << 16);
  844. /* value */
  845. if (p_264->open_gop) {
  846. shm &= ~(0xFFFF);
  847. shm |= p_264->open_gop_size;
  848. }
  849. s5p_mfc_write_info_v5(ctx, shm, H264_I_PERIOD);
  850. /* extended encoder ctrl */
  851. shm = s5p_mfc_read_info_v5(ctx, EXT_ENC_CONTROL);
  852. /* vbv buffer size */
  853. if (p->frame_skip_mode ==
  854. V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT) {
  855. shm &= ~(0xFFFF << 16);
  856. shm |= (p_264->cpb_size << 16);
  857. }
  858. s5p_mfc_write_info_v5(ctx, shm, EXT_ENC_CONTROL);
  859. return 0;
  860. }
  861. static int s5p_mfc_set_enc_params_mpeg4(struct s5p_mfc_ctx *ctx)
  862. {
  863. struct s5p_mfc_dev *dev = ctx->dev;
  864. struct s5p_mfc_enc_params *p = &ctx->enc_params;
  865. struct s5p_mfc_mpeg4_enc_params *p_mpeg4 = &p->codec.mpeg4;
  866. unsigned int reg;
  867. unsigned int shm;
  868. unsigned int framerate;
  869. s5p_mfc_set_enc_params(ctx);
  870. /* pictype : number of B */
  871. reg = mfc_read(dev, S5P_FIMV_ENC_PIC_TYPE_CTRL);
  872. /* num_b_frame - 0 ~ 2 */
  873. reg &= ~(0x3 << 16);
  874. reg |= (p->num_b_frame << 16);
  875. mfc_write(dev, reg, S5P_FIMV_ENC_PIC_TYPE_CTRL);
  876. /* profile & level */
  877. reg = mfc_read(dev, S5P_FIMV_ENC_PROFILE);
  878. /* level */
  879. reg &= ~(0xFF << 8);
  880. reg |= (p_mpeg4->level << 8);
  881. /* profile - 0 ~ 2 */
  882. reg &= ~(0x3F);
  883. reg |= p_mpeg4->profile;
  884. mfc_write(dev, reg, S5P_FIMV_ENC_PROFILE);
  885. /* quarter_pixel */
  886. mfc_write(dev, p_mpeg4->quarter_pixel, S5P_FIMV_ENC_MPEG4_QUART_PXL);
  887. /* qp */
  888. if (!p->rc_frame) {
  889. shm = s5p_mfc_read_info_v5(ctx, P_B_FRAME_QP);
  890. shm &= ~(0xFFF);
  891. shm |= ((p_mpeg4->rc_b_frame_qp & 0x3F) << 6);
  892. shm |= (p_mpeg4->rc_p_frame_qp & 0x3F);
  893. s5p_mfc_write_info_v5(ctx, shm, P_B_FRAME_QP);
  894. }
  895. /* frame rate */
  896. if (p->rc_frame) {
  897. if (p->rc_framerate_denom > 0) {
  898. framerate = p->rc_framerate_num * 1000 /
  899. p->rc_framerate_denom;
  900. mfc_write(dev, framerate,
  901. S5P_FIMV_ENC_RC_FRAME_RATE);
  902. shm = s5p_mfc_read_info_v5(ctx, RC_VOP_TIMING);
  903. shm &= ~(0xFFFFFFFF);
  904. shm |= (1 << 31);
  905. shm |= ((p->rc_framerate_num & 0x7FFF) << 16);
  906. shm |= (p->rc_framerate_denom & 0xFFFF);
  907. s5p_mfc_write_info_v5(ctx, shm, RC_VOP_TIMING);
  908. }
  909. } else {
  910. mfc_write(dev, 0, S5P_FIMV_ENC_RC_FRAME_RATE);
  911. }
  912. /* rate control config. */
  913. reg = mfc_read(dev, S5P_FIMV_ENC_RC_CONFIG);
  914. /* frame QP */
  915. reg &= ~(0x3F);
  916. reg |= p_mpeg4->rc_frame_qp;
  917. mfc_write(dev, reg, S5P_FIMV_ENC_RC_CONFIG);
  918. /* max & min value of QP */
  919. reg = mfc_read(dev, S5P_FIMV_ENC_RC_QBOUND);
  920. /* max QP */
  921. reg &= ~(0x3F << 8);
  922. reg |= (p_mpeg4->rc_max_qp << 8);
  923. /* min QP */
  924. reg &= ~(0x3F);
  925. reg |= p_mpeg4->rc_min_qp;
  926. mfc_write(dev, reg, S5P_FIMV_ENC_RC_QBOUND);
  927. /* extended encoder ctrl */
  928. shm = s5p_mfc_read_info_v5(ctx, EXT_ENC_CONTROL);
  929. /* vbv buffer size */
  930. if (p->frame_skip_mode ==
  931. V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT) {
  932. shm &= ~(0xFFFF << 16);
  933. shm |= (p->vbv_size << 16);
  934. }
  935. s5p_mfc_write_info_v5(ctx, shm, EXT_ENC_CONTROL);
  936. return 0;
  937. }
  938. static int s5p_mfc_set_enc_params_h263(struct s5p_mfc_ctx *ctx)
  939. {
  940. struct s5p_mfc_dev *dev = ctx->dev;
  941. struct s5p_mfc_enc_params *p = &ctx->enc_params;
  942. struct s5p_mfc_mpeg4_enc_params *p_h263 = &p->codec.mpeg4;
  943. unsigned int reg;
  944. unsigned int shm;
  945. s5p_mfc_set_enc_params(ctx);
  946. /* qp */
  947. if (!p->rc_frame) {
  948. shm = s5p_mfc_read_info_v5(ctx, P_B_FRAME_QP);
  949. shm &= ~(0xFFF);
  950. shm |= (p_h263->rc_p_frame_qp & 0x3F);
  951. s5p_mfc_write_info_v5(ctx, shm, P_B_FRAME_QP);
  952. }
  953. /* frame rate */
  954. if (p->rc_frame && p->rc_framerate_denom)
  955. mfc_write(dev, p->rc_framerate_num * 1000
  956. / p->rc_framerate_denom, S5P_FIMV_ENC_RC_FRAME_RATE);
  957. else
  958. mfc_write(dev, 0, S5P_FIMV_ENC_RC_FRAME_RATE);
  959. /* rate control config. */
  960. reg = mfc_read(dev, S5P_FIMV_ENC_RC_CONFIG);
  961. /* frame QP */
  962. reg &= ~(0x3F);
  963. reg |= p_h263->rc_frame_qp;
  964. mfc_write(dev, reg, S5P_FIMV_ENC_RC_CONFIG);
  965. /* max & min value of QP */
  966. reg = mfc_read(dev, S5P_FIMV_ENC_RC_QBOUND);
  967. /* max QP */
  968. reg &= ~(0x3F << 8);
  969. reg |= (p_h263->rc_max_qp << 8);
  970. /* min QP */
  971. reg &= ~(0x3F);
  972. reg |= p_h263->rc_min_qp;
  973. mfc_write(dev, reg, S5P_FIMV_ENC_RC_QBOUND);
  974. /* extended encoder ctrl */
  975. shm = s5p_mfc_read_info_v5(ctx, EXT_ENC_CONTROL);
  976. /* vbv buffer size */
  977. if (p->frame_skip_mode ==
  978. V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT) {
  979. shm &= ~(0xFFFF << 16);
  980. shm |= (p->vbv_size << 16);
  981. }
  982. s5p_mfc_write_info_v5(ctx, shm, EXT_ENC_CONTROL);
  983. return 0;
  984. }
  985. /* Initialize decoding */
  986. static int s5p_mfc_init_decode_v5(struct s5p_mfc_ctx *ctx)
  987. {
  988. struct s5p_mfc_dev *dev = ctx->dev;
  989. s5p_mfc_set_shared_buffer(ctx);
  990. /* Setup loop filter, for decoding this is only valid for MPEG4 */
  991. if (ctx->codec_mode == S5P_MFC_CODEC_MPEG4_DEC)
  992. mfc_write(dev, ctx->loop_filter_mpeg4, S5P_FIMV_ENC_LF_CTRL);
  993. else
  994. mfc_write(dev, 0, S5P_FIMV_ENC_LF_CTRL);
  995. mfc_write(dev, ((ctx->slice_interface & S5P_FIMV_SLICE_INT_MASK) <<
  996. S5P_FIMV_SLICE_INT_SHIFT) | (ctx->display_delay_enable <<
  997. S5P_FIMV_DDELAY_ENA_SHIFT) | ((ctx->display_delay &
  998. S5P_FIMV_DDELAY_VAL_MASK) << S5P_FIMV_DDELAY_VAL_SHIFT),
  999. S5P_FIMV_SI_CH0_DPB_CONF_CTRL);
  1000. mfc_write(dev,
  1001. ((S5P_FIMV_CH_SEQ_HEADER & S5P_FIMV_CH_MASK) << S5P_FIMV_CH_SHIFT)
  1002. | (ctx->inst_no), S5P_FIMV_SI_CH0_INST_ID);
  1003. return 0;
  1004. }
  1005. static void s5p_mfc_set_flush(struct s5p_mfc_ctx *ctx, int flush)
  1006. {
  1007. struct s5p_mfc_dev *dev = ctx->dev;
  1008. unsigned int dpb;
  1009. if (flush)
  1010. dpb = mfc_read(dev, S5P_FIMV_SI_CH0_DPB_CONF_CTRL) | (
  1011. S5P_FIMV_DPB_FLUSH_MASK << S5P_FIMV_DPB_FLUSH_SHIFT);
  1012. else
  1013. dpb = mfc_read(dev, S5P_FIMV_SI_CH0_DPB_CONF_CTRL) &
  1014. ~(S5P_FIMV_DPB_FLUSH_MASK << S5P_FIMV_DPB_FLUSH_SHIFT);
  1015. mfc_write(dev, dpb, S5P_FIMV_SI_CH0_DPB_CONF_CTRL);
  1016. }
  1017. /* Decode a single frame */
  1018. static int s5p_mfc_decode_one_frame_v5(struct s5p_mfc_ctx *ctx,
  1019. enum s5p_mfc_decode_arg last_frame)
  1020. {
  1021. struct s5p_mfc_dev *dev = ctx->dev;
  1022. mfc_write(dev, ctx->dec_dst_flag, S5P_FIMV_SI_CH0_RELEASE_BUF);
  1023. s5p_mfc_set_shared_buffer(ctx);
  1024. s5p_mfc_set_flush(ctx, ctx->dpb_flush_flag);
  1025. /* Issue different commands to instance basing on whether it
  1026. * is the last frame or not. */
  1027. switch (last_frame) {
  1028. case MFC_DEC_FRAME:
  1029. mfc_write(dev, ((S5P_FIMV_CH_FRAME_START & S5P_FIMV_CH_MASK) <<
  1030. S5P_FIMV_CH_SHIFT) | (ctx->inst_no), S5P_FIMV_SI_CH0_INST_ID);
  1031. break;
  1032. case MFC_DEC_LAST_FRAME:
  1033. mfc_write(dev, ((S5P_FIMV_CH_LAST_FRAME & S5P_FIMV_CH_MASK) <<
  1034. S5P_FIMV_CH_SHIFT) | (ctx->inst_no), S5P_FIMV_SI_CH0_INST_ID);
  1035. break;
  1036. case MFC_DEC_RES_CHANGE:
  1037. mfc_write(dev, ((S5P_FIMV_CH_FRAME_START_REALLOC &
  1038. S5P_FIMV_CH_MASK) << S5P_FIMV_CH_SHIFT) | (ctx->inst_no),
  1039. S5P_FIMV_SI_CH0_INST_ID);
  1040. break;
  1041. }
  1042. mfc_debug(2, "Decoding a usual frame\n");
  1043. return 0;
  1044. }
  1045. static int s5p_mfc_init_encode_v5(struct s5p_mfc_ctx *ctx)
  1046. {
  1047. struct s5p_mfc_dev *dev = ctx->dev;
  1048. if (ctx->codec_mode == S5P_MFC_CODEC_H264_ENC)
  1049. s5p_mfc_set_enc_params_h264(ctx);
  1050. else if (ctx->codec_mode == S5P_MFC_CODEC_MPEG4_ENC)
  1051. s5p_mfc_set_enc_params_mpeg4(ctx);
  1052. else if (ctx->codec_mode == S5P_MFC_CODEC_H263_ENC)
  1053. s5p_mfc_set_enc_params_h263(ctx);
  1054. else {
  1055. mfc_err("Unknown codec for encoding (%x)\n",
  1056. ctx->codec_mode);
  1057. return -EINVAL;
  1058. }
  1059. s5p_mfc_set_shared_buffer(ctx);
  1060. mfc_write(dev, ((S5P_FIMV_CH_SEQ_HEADER << 16) & 0x70000) |
  1061. (ctx->inst_no), S5P_FIMV_SI_CH0_INST_ID);
  1062. return 0;
  1063. }
  1064. /* Encode a single frame */
  1065. static int s5p_mfc_encode_one_frame_v5(struct s5p_mfc_ctx *ctx)
  1066. {
  1067. struct s5p_mfc_dev *dev = ctx->dev;
  1068. int cmd;
  1069. /* memory structure cur. frame */
  1070. if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12M)
  1071. mfc_write(dev, 0, S5P_FIMV_ENC_MAP_FOR_CUR);
  1072. else if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12MT)
  1073. mfc_write(dev, 3, S5P_FIMV_ENC_MAP_FOR_CUR);
  1074. s5p_mfc_set_shared_buffer(ctx);
  1075. if (ctx->state == MFCINST_FINISHING)
  1076. cmd = S5P_FIMV_CH_LAST_FRAME;
  1077. else
  1078. cmd = S5P_FIMV_CH_FRAME_START;
  1079. mfc_write(dev, ((cmd & S5P_FIMV_CH_MASK) << S5P_FIMV_CH_SHIFT)
  1080. | (ctx->inst_no), S5P_FIMV_SI_CH0_INST_ID);
  1081. return 0;
  1082. }
  1083. static void s5p_mfc_run_res_change(struct s5p_mfc_ctx *ctx)
  1084. {
  1085. struct s5p_mfc_dev *dev = ctx->dev;
  1086. s5p_mfc_set_dec_stream_buffer_v5(ctx, 0, 0, 0);
  1087. dev->curr_ctx = ctx->num;
  1088. s5p_mfc_decode_one_frame_v5(ctx, MFC_DEC_RES_CHANGE);
  1089. }
  1090. static int s5p_mfc_run_dec_frame(struct s5p_mfc_ctx *ctx, int last_frame)
  1091. {
  1092. struct s5p_mfc_dev *dev = ctx->dev;
  1093. struct s5p_mfc_buf *temp_vb;
  1094. if (ctx->state == MFCINST_FINISHING) {
  1095. last_frame = MFC_DEC_LAST_FRAME;
  1096. s5p_mfc_set_dec_stream_buffer_v5(ctx, 0, 0, 0);
  1097. dev->curr_ctx = ctx->num;
  1098. s5p_mfc_decode_one_frame_v5(ctx, last_frame);
  1099. return 0;
  1100. }
  1101. /* Frames are being decoded */
  1102. if (list_empty(&ctx->src_queue)) {
  1103. mfc_debug(2, "No src buffers\n");
  1104. return -EAGAIN;
  1105. }
  1106. /* Get the next source buffer */
  1107. temp_vb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
  1108. temp_vb->flags |= MFC_BUF_FLAG_USED;
  1109. s5p_mfc_set_dec_stream_buffer_v5(ctx,
  1110. vb2_dma_contig_plane_dma_addr(&temp_vb->b->vb2_buf, 0),
  1111. ctx->consumed_stream, temp_vb->b->vb2_buf.planes[0].bytesused);
  1112. dev->curr_ctx = ctx->num;
  1113. if (temp_vb->b->vb2_buf.planes[0].bytesused == 0) {
  1114. last_frame = MFC_DEC_LAST_FRAME;
  1115. mfc_debug(2, "Setting ctx->state to FINISHING\n");
  1116. ctx->state = MFCINST_FINISHING;
  1117. }
  1118. s5p_mfc_decode_one_frame_v5(ctx, last_frame);
  1119. return 0;
  1120. }
  1121. static int s5p_mfc_run_enc_frame(struct s5p_mfc_ctx *ctx)
  1122. {
  1123. struct s5p_mfc_dev *dev = ctx->dev;
  1124. struct s5p_mfc_buf *dst_mb;
  1125. struct s5p_mfc_buf *src_mb;
  1126. unsigned long src_y_addr, src_c_addr, dst_addr;
  1127. unsigned int dst_size;
  1128. if (list_empty(&ctx->src_queue) && ctx->state != MFCINST_FINISHING) {
  1129. mfc_debug(2, "no src buffers\n");
  1130. return -EAGAIN;
  1131. }
  1132. if (list_empty(&ctx->dst_queue)) {
  1133. mfc_debug(2, "no dst buffers\n");
  1134. return -EAGAIN;
  1135. }
  1136. if (list_empty(&ctx->src_queue)) {
  1137. /* send null frame */
  1138. s5p_mfc_set_enc_frame_buffer_v5(ctx, dev->dma_base[BANK_R_CTX],
  1139. dev->dma_base[BANK_R_CTX]);
  1140. src_mb = NULL;
  1141. } else {
  1142. src_mb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
  1143. list);
  1144. src_mb->flags |= MFC_BUF_FLAG_USED;
  1145. if (src_mb->b->vb2_buf.planes[0].bytesused == 0) {
  1146. /* send null frame */
  1147. s5p_mfc_set_enc_frame_buffer_v5(ctx,
  1148. dev->dma_base[BANK_R_CTX],
  1149. dev->dma_base[BANK_R_CTX]);
  1150. ctx->state = MFCINST_FINISHING;
  1151. } else {
  1152. src_y_addr = vb2_dma_contig_plane_dma_addr(
  1153. &src_mb->b->vb2_buf, 0);
  1154. src_c_addr = vb2_dma_contig_plane_dma_addr(
  1155. &src_mb->b->vb2_buf, 1);
  1156. s5p_mfc_set_enc_frame_buffer_v5(ctx, src_y_addr,
  1157. src_c_addr);
  1158. if (src_mb->flags & MFC_BUF_FLAG_EOS)
  1159. ctx->state = MFCINST_FINISHING;
  1160. }
  1161. }
  1162. dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
  1163. dst_mb->flags |= MFC_BUF_FLAG_USED;
  1164. dst_addr = vb2_dma_contig_plane_dma_addr(&dst_mb->b->vb2_buf, 0);
  1165. dst_size = vb2_plane_size(&dst_mb->b->vb2_buf, 0);
  1166. s5p_mfc_set_enc_stream_buffer_v5(ctx, dst_addr, dst_size);
  1167. dev->curr_ctx = ctx->num;
  1168. mfc_debug(2, "encoding buffer with index=%d state=%d\n",
  1169. src_mb ? src_mb->b->vb2_buf.index : -1, ctx->state);
  1170. s5p_mfc_encode_one_frame_v5(ctx);
  1171. return 0;
  1172. }
  1173. static void s5p_mfc_run_init_dec(struct s5p_mfc_ctx *ctx)
  1174. {
  1175. struct s5p_mfc_dev *dev = ctx->dev;
  1176. struct s5p_mfc_buf *temp_vb;
  1177. /* Initializing decoding - parsing header */
  1178. mfc_debug(2, "Preparing to init decoding\n");
  1179. temp_vb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
  1180. s5p_mfc_set_dec_desc_buffer(ctx);
  1181. mfc_debug(2, "Header size: %d\n",
  1182. temp_vb->b->vb2_buf.planes[0].bytesused);
  1183. s5p_mfc_set_dec_stream_buffer_v5(ctx,
  1184. vb2_dma_contig_plane_dma_addr(&temp_vb->b->vb2_buf, 0),
  1185. 0, temp_vb->b->vb2_buf.planes[0].bytesused);
  1186. dev->curr_ctx = ctx->num;
  1187. s5p_mfc_init_decode_v5(ctx);
  1188. }
  1189. static void s5p_mfc_run_init_enc(struct s5p_mfc_ctx *ctx)
  1190. {
  1191. struct s5p_mfc_dev *dev = ctx->dev;
  1192. struct s5p_mfc_buf *dst_mb;
  1193. unsigned long dst_addr;
  1194. unsigned int dst_size;
  1195. s5p_mfc_set_enc_ref_buffer_v5(ctx);
  1196. dst_mb = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf, list);
  1197. dst_addr = vb2_dma_contig_plane_dma_addr(&dst_mb->b->vb2_buf, 0);
  1198. dst_size = vb2_plane_size(&dst_mb->b->vb2_buf, 0);
  1199. s5p_mfc_set_enc_stream_buffer_v5(ctx, dst_addr, dst_size);
  1200. dev->curr_ctx = ctx->num;
  1201. s5p_mfc_init_encode_v5(ctx);
  1202. }
  1203. static int s5p_mfc_run_init_dec_buffers(struct s5p_mfc_ctx *ctx)
  1204. {
  1205. struct s5p_mfc_dev *dev = ctx->dev;
  1206. struct s5p_mfc_buf *temp_vb;
  1207. int ret;
  1208. /*
  1209. * Header was parsed now starting processing
  1210. * First set the output frame buffers
  1211. */
  1212. if (ctx->capture_state != QUEUE_BUFS_MMAPED) {
  1213. mfc_err("It seems that not all destionation buffers were mmaped\nMFC requires that all destination are mmaped before starting processing\n");
  1214. return -EAGAIN;
  1215. }
  1216. if (list_empty(&ctx->src_queue)) {
  1217. mfc_err("Header has been deallocated in the middle of initialization\n");
  1218. return -EIO;
  1219. }
  1220. temp_vb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
  1221. mfc_debug(2, "Header size: %d\n",
  1222. temp_vb->b->vb2_buf.planes[0].bytesused);
  1223. s5p_mfc_set_dec_stream_buffer_v5(ctx,
  1224. vb2_dma_contig_plane_dma_addr(&temp_vb->b->vb2_buf, 0),
  1225. 0, temp_vb->b->vb2_buf.planes[0].bytesused);
  1226. dev->curr_ctx = ctx->num;
  1227. ret = s5p_mfc_set_dec_frame_buffer_v5(ctx);
  1228. if (ret) {
  1229. mfc_err("Failed to alloc frame mem\n");
  1230. ctx->state = MFCINST_ERROR;
  1231. }
  1232. return ret;
  1233. }
  1234. /* Try running an operation on hardware */
  1235. static void s5p_mfc_try_run_v5(struct s5p_mfc_dev *dev)
  1236. {
  1237. struct s5p_mfc_ctx *ctx;
  1238. int new_ctx;
  1239. unsigned int ret = 0;
  1240. if (test_bit(0, &dev->enter_suspend)) {
  1241. mfc_debug(1, "Entering suspend so do not schedule any jobs\n");
  1242. return;
  1243. }
  1244. /* Check whether hardware is not running */
  1245. if (test_and_set_bit(0, &dev->hw_lock) != 0) {
  1246. /* This is perfectly ok, the scheduled ctx should wait */
  1247. mfc_debug(1, "Couldn't lock HW\n");
  1248. return;
  1249. }
  1250. /* Choose the context to run */
  1251. new_ctx = s5p_mfc_get_new_ctx(dev);
  1252. if (new_ctx < 0) {
  1253. /* No contexts to run */
  1254. if (test_and_clear_bit(0, &dev->hw_lock) == 0) {
  1255. mfc_err("Failed to unlock hardware\n");
  1256. return;
  1257. }
  1258. mfc_debug(1, "No ctx is scheduled to be run\n");
  1259. return;
  1260. }
  1261. ctx = dev->ctx[new_ctx];
  1262. /* Got context to run in ctx */
  1263. /*
  1264. * Last frame has already been sent to MFC.
  1265. * Now obtaining frames from MFC buffer
  1266. */
  1267. s5p_mfc_clock_on();
  1268. s5p_mfc_clean_ctx_int_flags(ctx);
  1269. if (ctx->type == MFCINST_DECODER) {
  1270. s5p_mfc_set_dec_desc_buffer(ctx);
  1271. switch (ctx->state) {
  1272. case MFCINST_FINISHING:
  1273. s5p_mfc_run_dec_frame(ctx, MFC_DEC_LAST_FRAME);
  1274. break;
  1275. case MFCINST_RUNNING:
  1276. ret = s5p_mfc_run_dec_frame(ctx, MFC_DEC_FRAME);
  1277. break;
  1278. case MFCINST_INIT:
  1279. ret = s5p_mfc_hw_call(dev->mfc_cmds, open_inst_cmd,
  1280. ctx);
  1281. break;
  1282. case MFCINST_RETURN_INST:
  1283. ret = s5p_mfc_hw_call(dev->mfc_cmds, close_inst_cmd,
  1284. ctx);
  1285. break;
  1286. case MFCINST_GOT_INST:
  1287. s5p_mfc_run_init_dec(ctx);
  1288. break;
  1289. case MFCINST_HEAD_PARSED:
  1290. ret = s5p_mfc_run_init_dec_buffers(ctx);
  1291. mfc_debug(1, "head parsed\n");
  1292. break;
  1293. case MFCINST_RES_CHANGE_INIT:
  1294. s5p_mfc_run_res_change(ctx);
  1295. break;
  1296. case MFCINST_RES_CHANGE_FLUSH:
  1297. s5p_mfc_run_dec_frame(ctx, MFC_DEC_FRAME);
  1298. break;
  1299. case MFCINST_RES_CHANGE_END:
  1300. mfc_debug(2, "Finished remaining frames after resolution change\n");
  1301. ctx->capture_state = QUEUE_FREE;
  1302. mfc_debug(2, "Will re-init the codec\n");
  1303. s5p_mfc_run_init_dec(ctx);
  1304. break;
  1305. default:
  1306. ret = -EAGAIN;
  1307. }
  1308. } else if (ctx->type == MFCINST_ENCODER) {
  1309. switch (ctx->state) {
  1310. case MFCINST_FINISHING:
  1311. case MFCINST_RUNNING:
  1312. ret = s5p_mfc_run_enc_frame(ctx);
  1313. break;
  1314. case MFCINST_INIT:
  1315. ret = s5p_mfc_hw_call(dev->mfc_cmds, open_inst_cmd,
  1316. ctx);
  1317. break;
  1318. case MFCINST_RETURN_INST:
  1319. ret = s5p_mfc_hw_call(dev->mfc_cmds, close_inst_cmd,
  1320. ctx);
  1321. break;
  1322. case MFCINST_GOT_INST:
  1323. s5p_mfc_run_init_enc(ctx);
  1324. break;
  1325. default:
  1326. ret = -EAGAIN;
  1327. }
  1328. } else {
  1329. mfc_err("Invalid context type: %d\n", ctx->type);
  1330. ret = -EAGAIN;
  1331. }
  1332. if (ret) {
  1333. /* Free hardware lock */
  1334. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  1335. mfc_err("Failed to unlock hardware\n");
  1336. /* This is in deed imporant, as no operation has been
  1337. * scheduled, reduce the clock count as no one will
  1338. * ever do this, because no interrupt related to this try_run
  1339. * will ever come from hardware. */
  1340. s5p_mfc_clock_off();
  1341. }
  1342. }
  1343. static void s5p_mfc_clear_int_flags_v5(struct s5p_mfc_dev *dev)
  1344. {
  1345. mfc_write(dev, 0, S5P_FIMV_RISC_HOST_INT);
  1346. mfc_write(dev, 0, S5P_FIMV_RISC2HOST_CMD);
  1347. mfc_write(dev, 0xffff, S5P_FIMV_SI_RTN_CHID);
  1348. }
  1349. static int s5p_mfc_get_dspl_y_adr_v5(struct s5p_mfc_dev *dev)
  1350. {
  1351. return mfc_read(dev, S5P_FIMV_SI_DISPLAY_Y_ADR) << MFC_OFFSET_SHIFT;
  1352. }
  1353. static int s5p_mfc_get_dec_y_adr_v5(struct s5p_mfc_dev *dev)
  1354. {
  1355. return mfc_read(dev, S5P_FIMV_SI_DECODE_Y_ADR) << MFC_OFFSET_SHIFT;
  1356. }
  1357. static int s5p_mfc_get_dspl_status_v5(struct s5p_mfc_dev *dev)
  1358. {
  1359. return mfc_read(dev, S5P_FIMV_SI_DISPLAY_STATUS);
  1360. }
  1361. static int s5p_mfc_get_dec_status_v5(struct s5p_mfc_dev *dev)
  1362. {
  1363. return mfc_read(dev, S5P_FIMV_SI_DECODE_STATUS);
  1364. }
  1365. static int s5p_mfc_get_dec_frame_type_v5(struct s5p_mfc_dev *dev)
  1366. {
  1367. return mfc_read(dev, S5P_FIMV_DECODE_FRAME_TYPE) &
  1368. S5P_FIMV_DECODE_FRAME_MASK;
  1369. }
  1370. static int s5p_mfc_get_disp_frame_type_v5(struct s5p_mfc_ctx *ctx)
  1371. {
  1372. return (s5p_mfc_read_info_v5(ctx, DISP_PIC_FRAME_TYPE) >>
  1373. S5P_FIMV_SHARED_DISP_FRAME_TYPE_SHIFT) &
  1374. S5P_FIMV_DECODE_FRAME_MASK;
  1375. }
  1376. static int s5p_mfc_get_consumed_stream_v5(struct s5p_mfc_dev *dev)
  1377. {
  1378. return mfc_read(dev, S5P_FIMV_SI_CONSUMED_BYTES);
  1379. }
  1380. static int s5p_mfc_get_int_reason_v5(struct s5p_mfc_dev *dev)
  1381. {
  1382. int reason;
  1383. reason = mfc_read(dev, S5P_FIMV_RISC2HOST_CMD) &
  1384. S5P_FIMV_RISC2HOST_CMD_MASK;
  1385. switch (reason) {
  1386. case S5P_FIMV_R2H_CMD_OPEN_INSTANCE_RET:
  1387. reason = S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET;
  1388. break;
  1389. case S5P_FIMV_R2H_CMD_CLOSE_INSTANCE_RET:
  1390. reason = S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET;
  1391. break;
  1392. case S5P_FIMV_R2H_CMD_SEQ_DONE_RET:
  1393. reason = S5P_MFC_R2H_CMD_SEQ_DONE_RET;
  1394. break;
  1395. case S5P_FIMV_R2H_CMD_FRAME_DONE_RET:
  1396. reason = S5P_MFC_R2H_CMD_FRAME_DONE_RET;
  1397. break;
  1398. case S5P_FIMV_R2H_CMD_SLICE_DONE_RET:
  1399. reason = S5P_MFC_R2H_CMD_SLICE_DONE_RET;
  1400. break;
  1401. case S5P_FIMV_R2H_CMD_SYS_INIT_RET:
  1402. reason = S5P_MFC_R2H_CMD_SYS_INIT_RET;
  1403. break;
  1404. case S5P_FIMV_R2H_CMD_FW_STATUS_RET:
  1405. reason = S5P_MFC_R2H_CMD_FW_STATUS_RET;
  1406. break;
  1407. case S5P_FIMV_R2H_CMD_SLEEP_RET:
  1408. reason = S5P_MFC_R2H_CMD_SLEEP_RET;
  1409. break;
  1410. case S5P_FIMV_R2H_CMD_WAKEUP_RET:
  1411. reason = S5P_MFC_R2H_CMD_WAKEUP_RET;
  1412. break;
  1413. case S5P_FIMV_R2H_CMD_INIT_BUFFERS_RET:
  1414. reason = S5P_MFC_R2H_CMD_INIT_BUFFERS_RET;
  1415. break;
  1416. case S5P_FIMV_R2H_CMD_ENC_COMPLETE_RET:
  1417. reason = S5P_MFC_R2H_CMD_COMPLETE_SEQ_RET;
  1418. break;
  1419. case S5P_FIMV_R2H_CMD_ERR_RET:
  1420. reason = S5P_MFC_R2H_CMD_ERR_RET;
  1421. break;
  1422. default:
  1423. reason = S5P_MFC_R2H_CMD_EMPTY;
  1424. }
  1425. return reason;
  1426. }
  1427. static int s5p_mfc_get_int_err_v5(struct s5p_mfc_dev *dev)
  1428. {
  1429. return mfc_read(dev, S5P_FIMV_RISC2HOST_ARG2);
  1430. }
  1431. static int s5p_mfc_err_dec_v5(unsigned int err)
  1432. {
  1433. return (err & S5P_FIMV_ERR_DEC_MASK) >> S5P_FIMV_ERR_DEC_SHIFT;
  1434. }
  1435. static int s5p_mfc_get_img_width_v5(struct s5p_mfc_dev *dev)
  1436. {
  1437. return mfc_read(dev, S5P_FIMV_SI_HRESOL);
  1438. }
  1439. static int s5p_mfc_get_img_height_v5(struct s5p_mfc_dev *dev)
  1440. {
  1441. return mfc_read(dev, S5P_FIMV_SI_VRESOL);
  1442. }
  1443. static int s5p_mfc_get_dpb_count_v5(struct s5p_mfc_dev *dev)
  1444. {
  1445. return mfc_read(dev, S5P_FIMV_SI_BUF_NUMBER);
  1446. }
  1447. static int s5p_mfc_get_mv_count_v5(struct s5p_mfc_dev *dev)
  1448. {
  1449. /* NOP */
  1450. return -1;
  1451. }
  1452. static int s5p_mfc_get_inst_no_v5(struct s5p_mfc_dev *dev)
  1453. {
  1454. return mfc_read(dev, S5P_FIMV_RISC2HOST_ARG1);
  1455. }
  1456. static int s5p_mfc_get_enc_strm_size_v5(struct s5p_mfc_dev *dev)
  1457. {
  1458. return mfc_read(dev, S5P_FIMV_ENC_SI_STRM_SIZE);
  1459. }
  1460. static int s5p_mfc_get_enc_slice_type_v5(struct s5p_mfc_dev *dev)
  1461. {
  1462. return mfc_read(dev, S5P_FIMV_ENC_SI_SLICE_TYPE);
  1463. }
  1464. static int s5p_mfc_get_enc_dpb_count_v5(struct s5p_mfc_dev *dev)
  1465. {
  1466. return -1;
  1467. }
  1468. static unsigned int s5p_mfc_get_pic_type_top_v5(struct s5p_mfc_ctx *ctx)
  1469. {
  1470. return s5p_mfc_read_info_v5(ctx, PIC_TIME_TOP);
  1471. }
  1472. static unsigned int s5p_mfc_get_pic_type_bot_v5(struct s5p_mfc_ctx *ctx)
  1473. {
  1474. return s5p_mfc_read_info_v5(ctx, PIC_TIME_BOT);
  1475. }
  1476. static unsigned int s5p_mfc_get_crop_info_h_v5(struct s5p_mfc_ctx *ctx)
  1477. {
  1478. return s5p_mfc_read_info_v5(ctx, CROP_INFO_H);
  1479. }
  1480. static unsigned int s5p_mfc_get_crop_info_v_v5(struct s5p_mfc_ctx *ctx)
  1481. {
  1482. return s5p_mfc_read_info_v5(ctx, CROP_INFO_V);
  1483. }
  1484. /* Initialize opr function pointers for MFC v5 */
  1485. static struct s5p_mfc_hw_ops s5p_mfc_ops_v5 = {
  1486. .alloc_dec_temp_buffers = s5p_mfc_alloc_dec_temp_buffers_v5,
  1487. .release_dec_desc_buffer = s5p_mfc_release_dec_desc_buffer_v5,
  1488. .alloc_codec_buffers = s5p_mfc_alloc_codec_buffers_v5,
  1489. .release_codec_buffers = s5p_mfc_release_codec_buffers_v5,
  1490. .alloc_instance_buffer = s5p_mfc_alloc_instance_buffer_v5,
  1491. .release_instance_buffer = s5p_mfc_release_instance_buffer_v5,
  1492. .alloc_dev_context_buffer = s5p_mfc_alloc_dev_context_buffer_v5,
  1493. .release_dev_context_buffer = s5p_mfc_release_dev_context_buffer_v5,
  1494. .dec_calc_dpb_size = s5p_mfc_dec_calc_dpb_size_v5,
  1495. .enc_calc_src_size = s5p_mfc_enc_calc_src_size_v5,
  1496. .set_enc_stream_buffer = s5p_mfc_set_enc_stream_buffer_v5,
  1497. .set_enc_frame_buffer = s5p_mfc_set_enc_frame_buffer_v5,
  1498. .get_enc_frame_buffer = s5p_mfc_get_enc_frame_buffer_v5,
  1499. .try_run = s5p_mfc_try_run_v5,
  1500. .clear_int_flags = s5p_mfc_clear_int_flags_v5,
  1501. .get_dspl_y_adr = s5p_mfc_get_dspl_y_adr_v5,
  1502. .get_dec_y_adr = s5p_mfc_get_dec_y_adr_v5,
  1503. .get_dspl_status = s5p_mfc_get_dspl_status_v5,
  1504. .get_dec_status = s5p_mfc_get_dec_status_v5,
  1505. .get_dec_frame_type = s5p_mfc_get_dec_frame_type_v5,
  1506. .get_disp_frame_type = s5p_mfc_get_disp_frame_type_v5,
  1507. .get_consumed_stream = s5p_mfc_get_consumed_stream_v5,
  1508. .get_int_reason = s5p_mfc_get_int_reason_v5,
  1509. .get_int_err = s5p_mfc_get_int_err_v5,
  1510. .err_dec = s5p_mfc_err_dec_v5,
  1511. .get_img_width = s5p_mfc_get_img_width_v5,
  1512. .get_img_height = s5p_mfc_get_img_height_v5,
  1513. .get_dpb_count = s5p_mfc_get_dpb_count_v5,
  1514. .get_mv_count = s5p_mfc_get_mv_count_v5,
  1515. .get_inst_no = s5p_mfc_get_inst_no_v5,
  1516. .get_enc_strm_size = s5p_mfc_get_enc_strm_size_v5,
  1517. .get_enc_slice_type = s5p_mfc_get_enc_slice_type_v5,
  1518. .get_enc_dpb_count = s5p_mfc_get_enc_dpb_count_v5,
  1519. .get_pic_type_top = s5p_mfc_get_pic_type_top_v5,
  1520. .get_pic_type_bot = s5p_mfc_get_pic_type_bot_v5,
  1521. .get_crop_info_h = s5p_mfc_get_crop_info_h_v5,
  1522. .get_crop_info_v = s5p_mfc_get_crop_info_v_v5,
  1523. };
  1524. struct s5p_mfc_hw_ops *s5p_mfc_init_hw_ops_v5(void)
  1525. {
  1526. return &s5p_mfc_ops_v5;
  1527. }