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->bank1) >> MFC_OFFSET_SHIFT)
  32. #define OFFSETB(x) (((x) - dev->bank2) >> 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->mem_dev_l, dev->bank1, &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->mem_dev_l, &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->mem_dev_l, dev->bank1,
  165. &ctx->bank1);
  166. if (ret) {
  167. mfc_err("Failed to allocate Bank1 temporary buffer\n");
  168. return ret;
  169. }
  170. BUG_ON(ctx->bank1.dma & ((1 << MFC_BANK1_ALIGN_ORDER) - 1));
  171. }
  172. /* Allocate only if memory from bank 2 is necessary */
  173. if (ctx->bank2.size > 0) {
  174. ret = s5p_mfc_alloc_priv_buf(dev->mem_dev_r, dev->bank2,
  175. &ctx->bank2);
  176. if (ret) {
  177. mfc_err("Failed to allocate Bank2 temporary buffer\n");
  178. s5p_mfc_release_priv_buf(ctx->dev->mem_dev_l, &ctx->bank1);
  179. return ret;
  180. }
  181. BUG_ON(ctx->bank2.dma & ((1 << MFC_BANK2_ALIGN_ORDER) - 1));
  182. }
  183. return 0;
  184. }
  185. /* Release buffers allocated for codec */
  186. static void s5p_mfc_release_codec_buffers_v5(struct s5p_mfc_ctx *ctx)
  187. {
  188. s5p_mfc_release_priv_buf(ctx->dev->mem_dev_l, &ctx->bank1);
  189. s5p_mfc_release_priv_buf(ctx->dev->mem_dev_r, &ctx->bank2);
  190. }
  191. /* Allocate memory for instance data buffer */
  192. static int s5p_mfc_alloc_instance_buffer_v5(struct s5p_mfc_ctx *ctx)
  193. {
  194. struct s5p_mfc_dev *dev = ctx->dev;
  195. struct s5p_mfc_buf_size_v5 *buf_size = dev->variant->buf_size->priv;
  196. int ret;
  197. if (ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
  198. ctx->codec_mode == S5P_MFC_CODEC_H264_ENC)
  199. ctx->ctx.size = buf_size->h264_ctx;
  200. else
  201. ctx->ctx.size = buf_size->non_h264_ctx;
  202. ret = s5p_mfc_alloc_priv_buf(dev->mem_dev_l, dev->bank1, &ctx->ctx);
  203. if (ret) {
  204. mfc_err("Failed to allocate instance buffer\n");
  205. return ret;
  206. }
  207. ctx->ctx.ofs = OFFSETA(ctx->ctx.dma);
  208. /* Zero content of the allocated memory */
  209. memset(ctx->ctx.virt, 0, ctx->ctx.size);
  210. wmb();
  211. /* Initialize shared memory */
  212. ctx->shm.size = buf_size->shm;
  213. ret = s5p_mfc_alloc_priv_buf(dev->mem_dev_l, dev->bank1, &ctx->shm);
  214. if (ret) {
  215. mfc_err("Failed to allocate shared memory buffer\n");
  216. s5p_mfc_release_priv_buf(dev->mem_dev_l, &ctx->ctx);
  217. return ret;
  218. }
  219. /* shared memory offset only keeps the offset from base (port a) */
  220. ctx->shm.ofs = ctx->shm.dma - dev->bank1;
  221. BUG_ON(ctx->shm.ofs & ((1 << MFC_BANK1_ALIGN_ORDER) - 1));
  222. memset(ctx->shm.virt, 0, buf_size->shm);
  223. wmb();
  224. return 0;
  225. }
  226. /* Release instance buffer */
  227. static void s5p_mfc_release_instance_buffer_v5(struct s5p_mfc_ctx *ctx)
  228. {
  229. s5p_mfc_release_priv_buf(ctx->dev->mem_dev_l, &ctx->ctx);
  230. s5p_mfc_release_priv_buf(ctx->dev->mem_dev_l, &ctx->shm);
  231. }
  232. static int s5p_mfc_alloc_dev_context_buffer_v5(struct s5p_mfc_dev *dev)
  233. {
  234. /* NOP */
  235. return 0;
  236. }
  237. static void s5p_mfc_release_dev_context_buffer_v5(struct s5p_mfc_dev *dev)
  238. {
  239. /* NOP */
  240. }
  241. static void s5p_mfc_write_info_v5(struct s5p_mfc_ctx *ctx, unsigned int data,
  242. unsigned int ofs)
  243. {
  244. *(u32 *)(ctx->shm.virt + ofs) = data;
  245. wmb();
  246. }
  247. static unsigned int s5p_mfc_read_info_v5(struct s5p_mfc_ctx *ctx,
  248. unsigned long ofs)
  249. {
  250. rmb();
  251. return *(u32 *)(ctx->shm.virt + ofs);
  252. }
  253. static void s5p_mfc_dec_calc_dpb_size_v5(struct s5p_mfc_ctx *ctx)
  254. {
  255. unsigned int guard_width, guard_height;
  256. ctx->buf_width = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN);
  257. ctx->buf_height = ALIGN(ctx->img_height, S5P_FIMV_NV12MT_VALIGN);
  258. mfc_debug(2,
  259. "SEQ Done: Movie dimensions %dx%d, buffer dimensions: %dx%d\n",
  260. ctx->img_width, ctx->img_height, ctx->buf_width,
  261. ctx->buf_height);
  262. if (ctx->codec_mode == S5P_MFC_CODEC_H264_DEC) {
  263. ctx->luma_size = ALIGN(ctx->buf_width * ctx->buf_height,
  264. S5P_FIMV_DEC_BUF_ALIGN);
  265. ctx->chroma_size = ALIGN(ctx->buf_width *
  266. ALIGN((ctx->img_height >> 1),
  267. S5P_FIMV_NV12MT_VALIGN),
  268. S5P_FIMV_DEC_BUF_ALIGN);
  269. ctx->mv_size = ALIGN(ctx->buf_width *
  270. ALIGN((ctx->buf_height >> 2),
  271. S5P_FIMV_NV12MT_VALIGN),
  272. S5P_FIMV_DEC_BUF_ALIGN);
  273. } else {
  274. guard_width =
  275. ALIGN(ctx->img_width + 24, S5P_FIMV_NV12MT_HALIGN);
  276. guard_height =
  277. ALIGN(ctx->img_height + 16, S5P_FIMV_NV12MT_VALIGN);
  278. ctx->luma_size = ALIGN(guard_width * guard_height,
  279. S5P_FIMV_DEC_BUF_ALIGN);
  280. guard_width =
  281. ALIGN(ctx->img_width + 16, S5P_FIMV_NV12MT_HALIGN);
  282. guard_height =
  283. ALIGN((ctx->img_height >> 1) + 4,
  284. S5P_FIMV_NV12MT_VALIGN);
  285. ctx->chroma_size = ALIGN(guard_width * guard_height,
  286. S5P_FIMV_DEC_BUF_ALIGN);
  287. ctx->mv_size = 0;
  288. }
  289. }
  290. static void s5p_mfc_enc_calc_src_size_v5(struct s5p_mfc_ctx *ctx)
  291. {
  292. if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12M) {
  293. ctx->buf_width = ALIGN(ctx->img_width, S5P_FIMV_NV12M_HALIGN);
  294. ctx->luma_size = ALIGN(ctx->img_width, S5P_FIMV_NV12M_HALIGN)
  295. * ALIGN(ctx->img_height, S5P_FIMV_NV12M_LVALIGN);
  296. ctx->chroma_size = ALIGN(ctx->img_width, S5P_FIMV_NV12M_HALIGN)
  297. * ALIGN((ctx->img_height >> 1), S5P_FIMV_NV12M_CVALIGN);
  298. ctx->luma_size = ALIGN(ctx->luma_size, S5P_FIMV_NV12M_SALIGN);
  299. ctx->chroma_size =
  300. ALIGN(ctx->chroma_size, S5P_FIMV_NV12M_SALIGN);
  301. } else if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12MT) {
  302. ctx->buf_width = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN);
  303. ctx->luma_size = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN)
  304. * ALIGN(ctx->img_height, S5P_FIMV_NV12MT_VALIGN);
  305. ctx->chroma_size =
  306. ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN)
  307. * ALIGN((ctx->img_height >> 1), S5P_FIMV_NV12MT_VALIGN);
  308. ctx->luma_size = ALIGN(ctx->luma_size, S5P_FIMV_NV12MT_SALIGN);
  309. ctx->chroma_size =
  310. ALIGN(ctx->chroma_size, S5P_FIMV_NV12MT_SALIGN);
  311. }
  312. }
  313. /* Set registers for decoding temporary buffers */
  314. static void s5p_mfc_set_dec_desc_buffer(struct s5p_mfc_ctx *ctx)
  315. {
  316. struct s5p_mfc_dev *dev = ctx->dev;
  317. struct s5p_mfc_buf_size_v5 *buf_size = dev->variant->buf_size->priv;
  318. mfc_write(dev, OFFSETA(ctx->dsc.dma), S5P_FIMV_SI_CH0_DESC_ADR);
  319. mfc_write(dev, buf_size->dsc, S5P_FIMV_SI_CH0_DESC_SIZE);
  320. }
  321. /* Set registers for shared buffer */
  322. static void s5p_mfc_set_shared_buffer(struct s5p_mfc_ctx *ctx)
  323. {
  324. struct s5p_mfc_dev *dev = ctx->dev;
  325. mfc_write(dev, ctx->shm.ofs, S5P_FIMV_SI_CH0_HOST_WR_ADR);
  326. }
  327. /* Set registers for decoding stream buffer */
  328. static int s5p_mfc_set_dec_stream_buffer_v5(struct s5p_mfc_ctx *ctx,
  329. int buf_addr, unsigned int start_num_byte,
  330. unsigned int buf_size)
  331. {
  332. struct s5p_mfc_dev *dev = ctx->dev;
  333. mfc_write(dev, OFFSETA(buf_addr), S5P_FIMV_SI_CH0_SB_ST_ADR);
  334. mfc_write(dev, ctx->dec_src_buf_size, S5P_FIMV_SI_CH0_CPB_SIZE);
  335. mfc_write(dev, buf_size, S5P_FIMV_SI_CH0_SB_FRM_SIZE);
  336. s5p_mfc_write_info_v5(ctx, start_num_byte, START_BYTE_NUM);
  337. return 0;
  338. }
  339. /* Set decoding frame buffer */
  340. static int s5p_mfc_set_dec_frame_buffer_v5(struct s5p_mfc_ctx *ctx)
  341. {
  342. unsigned int frame_size_lu, i;
  343. unsigned int frame_size_ch, frame_size_mv;
  344. struct s5p_mfc_dev *dev = ctx->dev;
  345. unsigned int dpb;
  346. size_t buf_addr1, buf_addr2;
  347. int buf_size1, buf_size2;
  348. buf_addr1 = ctx->bank1.dma;
  349. buf_size1 = ctx->bank1.size;
  350. buf_addr2 = ctx->bank2.dma;
  351. buf_size2 = ctx->bank2.size;
  352. dpb = mfc_read(dev, S5P_FIMV_SI_CH0_DPB_CONF_CTRL) &
  353. ~S5P_FIMV_DPB_COUNT_MASK;
  354. mfc_write(dev, ctx->total_dpb_count | dpb,
  355. S5P_FIMV_SI_CH0_DPB_CONF_CTRL);
  356. s5p_mfc_set_shared_buffer(ctx);
  357. switch (ctx->codec_mode) {
  358. case S5P_MFC_CODEC_H264_DEC:
  359. mfc_write(dev, OFFSETA(buf_addr1),
  360. S5P_FIMV_H264_VERT_NB_MV_ADR);
  361. buf_addr1 += S5P_FIMV_DEC_VERT_NB_MV_SIZE;
  362. buf_size1 -= S5P_FIMV_DEC_VERT_NB_MV_SIZE;
  363. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H264_NB_IP_ADR);
  364. buf_addr1 += S5P_FIMV_DEC_NB_IP_SIZE;
  365. buf_size1 -= S5P_FIMV_DEC_NB_IP_SIZE;
  366. break;
  367. case S5P_MFC_CODEC_MPEG4_DEC:
  368. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_MPEG4_NB_DCAC_ADR);
  369. buf_addr1 += S5P_FIMV_DEC_NB_DCAC_SIZE;
  370. buf_size1 -= S5P_FIMV_DEC_NB_DCAC_SIZE;
  371. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_MPEG4_UP_NB_MV_ADR);
  372. buf_addr1 += S5P_FIMV_DEC_UPNB_MV_SIZE;
  373. buf_size1 -= S5P_FIMV_DEC_UPNB_MV_SIZE;
  374. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_MPEG4_SA_MV_ADR);
  375. buf_addr1 += S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE;
  376. buf_size1 -= S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE;
  377. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_MPEG4_SP_ADR);
  378. buf_addr1 += S5P_FIMV_DEC_STX_PARSER_SIZE;
  379. buf_size1 -= S5P_FIMV_DEC_STX_PARSER_SIZE;
  380. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_MPEG4_OT_LINE_ADR);
  381. buf_addr1 += S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE;
  382. buf_size1 -= S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE;
  383. break;
  384. case S5P_MFC_CODEC_H263_DEC:
  385. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H263_OT_LINE_ADR);
  386. buf_addr1 += S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE;
  387. buf_size1 -= S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE;
  388. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H263_UP_NB_MV_ADR);
  389. buf_addr1 += S5P_FIMV_DEC_UPNB_MV_SIZE;
  390. buf_size1 -= S5P_FIMV_DEC_UPNB_MV_SIZE;
  391. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H263_SA_MV_ADR);
  392. buf_addr1 += S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE;
  393. buf_size1 -= S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE;
  394. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H263_NB_DCAC_ADR);
  395. buf_addr1 += S5P_FIMV_DEC_NB_DCAC_SIZE;
  396. buf_size1 -= S5P_FIMV_DEC_NB_DCAC_SIZE;
  397. break;
  398. case S5P_MFC_CODEC_VC1_DEC:
  399. case S5P_MFC_CODEC_VC1RCV_DEC:
  400. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_NB_DCAC_ADR);
  401. buf_addr1 += S5P_FIMV_DEC_NB_DCAC_SIZE;
  402. buf_size1 -= S5P_FIMV_DEC_NB_DCAC_SIZE;
  403. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_OT_LINE_ADR);
  404. buf_addr1 += S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE;
  405. buf_size1 -= S5P_FIMV_DEC_OVERLAP_TRANSFORM_SIZE;
  406. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_UP_NB_MV_ADR);
  407. buf_addr1 += S5P_FIMV_DEC_UPNB_MV_SIZE;
  408. buf_size1 -= S5P_FIMV_DEC_UPNB_MV_SIZE;
  409. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_SA_MV_ADR);
  410. buf_addr1 += S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE;
  411. buf_size1 -= S5P_FIMV_DEC_SUB_ANCHOR_MV_SIZE;
  412. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_BITPLANE3_ADR);
  413. buf_addr1 += S5P_FIMV_DEC_VC1_BITPLANE_SIZE;
  414. buf_size1 -= S5P_FIMV_DEC_VC1_BITPLANE_SIZE;
  415. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_BITPLANE2_ADR);
  416. buf_addr1 += S5P_FIMV_DEC_VC1_BITPLANE_SIZE;
  417. buf_size1 -= S5P_FIMV_DEC_VC1_BITPLANE_SIZE;
  418. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_VC1_BITPLANE1_ADR);
  419. buf_addr1 += S5P_FIMV_DEC_VC1_BITPLANE_SIZE;
  420. buf_size1 -= S5P_FIMV_DEC_VC1_BITPLANE_SIZE;
  421. break;
  422. case S5P_MFC_CODEC_MPEG2_DEC:
  423. break;
  424. default:
  425. mfc_err("Unknown codec for decoding (%x)\n",
  426. ctx->codec_mode);
  427. return -EINVAL;
  428. }
  429. frame_size_lu = ctx->luma_size;
  430. frame_size_ch = ctx->chroma_size;
  431. frame_size_mv = ctx->mv_size;
  432. mfc_debug(2, "Frm size: %d ch: %d mv: %d\n", frame_size_lu, frame_size_ch,
  433. frame_size_mv);
  434. for (i = 0; i < ctx->total_dpb_count; i++) {
  435. /* Bank2 */
  436. mfc_debug(2, "Luma %d: %zx\n", i,
  437. ctx->dst_bufs[i].cookie.raw.luma);
  438. mfc_write(dev, OFFSETB(ctx->dst_bufs[i].cookie.raw.luma),
  439. S5P_FIMV_DEC_LUMA_ADR + i * 4);
  440. mfc_debug(2, "\tChroma %d: %zx\n", i,
  441. ctx->dst_bufs[i].cookie.raw.chroma);
  442. mfc_write(dev, OFFSETA(ctx->dst_bufs[i].cookie.raw.chroma),
  443. S5P_FIMV_DEC_CHROMA_ADR + i * 4);
  444. if (ctx->codec_mode == S5P_MFC_CODEC_H264_DEC) {
  445. mfc_debug(2, "\tBuf2: %zx, size: %d\n",
  446. buf_addr2, buf_size2);
  447. mfc_write(dev, OFFSETB(buf_addr2),
  448. S5P_FIMV_H264_MV_ADR + i * 4);
  449. buf_addr2 += frame_size_mv;
  450. buf_size2 -= frame_size_mv;
  451. }
  452. }
  453. mfc_debug(2, "Buf1: %zu, buf_size1: %d\n", buf_addr1, buf_size1);
  454. mfc_debug(2, "Buf 1/2 size after: %d/%d (frames %d)\n",
  455. buf_size1, buf_size2, ctx->total_dpb_count);
  456. if (buf_size1 < 0 || buf_size2 < 0) {
  457. mfc_debug(2, "Not enough memory has been allocated\n");
  458. return -ENOMEM;
  459. }
  460. s5p_mfc_write_info_v5(ctx, frame_size_lu, ALLOC_LUMA_DPB_SIZE);
  461. s5p_mfc_write_info_v5(ctx, frame_size_ch, ALLOC_CHROMA_DPB_SIZE);
  462. if (ctx->codec_mode == S5P_MFC_CODEC_H264_DEC)
  463. s5p_mfc_write_info_v5(ctx, frame_size_mv, ALLOC_MV_SIZE);
  464. mfc_write(dev, ((S5P_FIMV_CH_INIT_BUFS & S5P_FIMV_CH_MASK)
  465. << S5P_FIMV_CH_SHIFT) | (ctx->inst_no),
  466. S5P_FIMV_SI_CH0_INST_ID);
  467. return 0;
  468. }
  469. /* Set registers for encoding stream buffer */
  470. static int s5p_mfc_set_enc_stream_buffer_v5(struct s5p_mfc_ctx *ctx,
  471. unsigned long addr, unsigned int size)
  472. {
  473. struct s5p_mfc_dev *dev = ctx->dev;
  474. mfc_write(dev, OFFSETA(addr), S5P_FIMV_ENC_SI_CH0_SB_ADR);
  475. mfc_write(dev, size, S5P_FIMV_ENC_SI_CH0_SB_SIZE);
  476. return 0;
  477. }
  478. static void s5p_mfc_set_enc_frame_buffer_v5(struct s5p_mfc_ctx *ctx,
  479. unsigned long y_addr, unsigned long c_addr)
  480. {
  481. struct s5p_mfc_dev *dev = ctx->dev;
  482. mfc_write(dev, OFFSETB(y_addr), S5P_FIMV_ENC_SI_CH0_CUR_Y_ADR);
  483. mfc_write(dev, OFFSETB(c_addr), S5P_FIMV_ENC_SI_CH0_CUR_C_ADR);
  484. }
  485. static void s5p_mfc_get_enc_frame_buffer_v5(struct s5p_mfc_ctx *ctx,
  486. unsigned long *y_addr, unsigned long *c_addr)
  487. {
  488. struct s5p_mfc_dev *dev = ctx->dev;
  489. *y_addr = dev->bank2 + (mfc_read(dev, S5P_FIMV_ENCODED_Y_ADDR)
  490. << MFC_OFFSET_SHIFT);
  491. *c_addr = dev->bank2 + (mfc_read(dev, S5P_FIMV_ENCODED_C_ADDR)
  492. << MFC_OFFSET_SHIFT);
  493. }
  494. /* Set encoding ref & codec buffer */
  495. static int s5p_mfc_set_enc_ref_buffer_v5(struct s5p_mfc_ctx *ctx)
  496. {
  497. struct s5p_mfc_dev *dev = ctx->dev;
  498. size_t buf_addr1, buf_addr2;
  499. size_t buf_size1, buf_size2;
  500. unsigned int enc_ref_y_size, enc_ref_c_size;
  501. unsigned int guard_width, guard_height;
  502. int i;
  503. buf_addr1 = ctx->bank1.dma;
  504. buf_size1 = ctx->bank1.size;
  505. buf_addr2 = ctx->bank2.dma;
  506. buf_size2 = ctx->bank2.size;
  507. enc_ref_y_size = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN)
  508. * ALIGN(ctx->img_height, S5P_FIMV_NV12MT_VALIGN);
  509. enc_ref_y_size = ALIGN(enc_ref_y_size, S5P_FIMV_NV12MT_SALIGN);
  510. if (ctx->codec_mode == S5P_MFC_CODEC_H264_ENC) {
  511. enc_ref_c_size = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN)
  512. * ALIGN((ctx->img_height >> 1), S5P_FIMV_NV12MT_VALIGN);
  513. enc_ref_c_size = ALIGN(enc_ref_c_size, S5P_FIMV_NV12MT_SALIGN);
  514. } else {
  515. guard_width = ALIGN(ctx->img_width + 16,
  516. S5P_FIMV_NV12MT_HALIGN);
  517. guard_height = ALIGN((ctx->img_height >> 1) + 4,
  518. S5P_FIMV_NV12MT_VALIGN);
  519. enc_ref_c_size = ALIGN(guard_width * guard_height,
  520. S5P_FIMV_NV12MT_SALIGN);
  521. }
  522. mfc_debug(2, "buf_size1: %zu, buf_size2: %zu\n", buf_size1, buf_size2);
  523. switch (ctx->codec_mode) {
  524. case S5P_MFC_CODEC_H264_ENC:
  525. for (i = 0; i < 2; i++) {
  526. mfc_write(dev, OFFSETA(buf_addr1),
  527. S5P_FIMV_ENC_REF0_LUMA_ADR + (4 * i));
  528. buf_addr1 += enc_ref_y_size;
  529. buf_size1 -= enc_ref_y_size;
  530. mfc_write(dev, OFFSETB(buf_addr2),
  531. S5P_FIMV_ENC_REF2_LUMA_ADR + (4 * i));
  532. buf_addr2 += enc_ref_y_size;
  533. buf_size2 -= enc_ref_y_size;
  534. }
  535. for (i = 0; i < 4; i++) {
  536. mfc_write(dev, OFFSETB(buf_addr2),
  537. S5P_FIMV_ENC_REF0_CHROMA_ADR + (4 * i));
  538. buf_addr2 += enc_ref_c_size;
  539. buf_size2 -= enc_ref_c_size;
  540. }
  541. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H264_UP_MV_ADR);
  542. buf_addr1 += S5P_FIMV_ENC_UPMV_SIZE;
  543. buf_size1 -= S5P_FIMV_ENC_UPMV_SIZE;
  544. mfc_write(dev, OFFSETA(buf_addr1),
  545. S5P_FIMV_H264_COZERO_FLAG_ADR);
  546. buf_addr1 += S5P_FIMV_ENC_COLFLG_SIZE;
  547. buf_size1 -= S5P_FIMV_ENC_COLFLG_SIZE;
  548. mfc_write(dev, OFFSETA(buf_addr1),
  549. S5P_FIMV_H264_UP_INTRA_MD_ADR);
  550. buf_addr1 += S5P_FIMV_ENC_INTRAMD_SIZE;
  551. buf_size1 -= S5P_FIMV_ENC_INTRAMD_SIZE;
  552. mfc_write(dev, OFFSETB(buf_addr2),
  553. S5P_FIMV_H264_UP_INTRA_PRED_ADR);
  554. buf_addr2 += S5P_FIMV_ENC_INTRAPRED_SIZE;
  555. buf_size2 -= S5P_FIMV_ENC_INTRAPRED_SIZE;
  556. mfc_write(dev, OFFSETA(buf_addr1),
  557. S5P_FIMV_H264_NBOR_INFO_ADR);
  558. buf_addr1 += S5P_FIMV_ENC_NBORINFO_SIZE;
  559. buf_size1 -= S5P_FIMV_ENC_NBORINFO_SIZE;
  560. mfc_debug(2, "buf_size1: %zu, buf_size2: %zu\n",
  561. buf_size1, buf_size2);
  562. break;
  563. case S5P_MFC_CODEC_MPEG4_ENC:
  564. for (i = 0; i < 2; i++) {
  565. mfc_write(dev, OFFSETA(buf_addr1),
  566. S5P_FIMV_ENC_REF0_LUMA_ADR + (4 * i));
  567. buf_addr1 += enc_ref_y_size;
  568. buf_size1 -= enc_ref_y_size;
  569. mfc_write(dev, OFFSETB(buf_addr2),
  570. S5P_FIMV_ENC_REF2_LUMA_ADR + (4 * i));
  571. buf_addr2 += enc_ref_y_size;
  572. buf_size2 -= enc_ref_y_size;
  573. }
  574. for (i = 0; i < 4; i++) {
  575. mfc_write(dev, OFFSETB(buf_addr2),
  576. S5P_FIMV_ENC_REF0_CHROMA_ADR + (4 * i));
  577. buf_addr2 += enc_ref_c_size;
  578. buf_size2 -= enc_ref_c_size;
  579. }
  580. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_MPEG4_UP_MV_ADR);
  581. buf_addr1 += S5P_FIMV_ENC_UPMV_SIZE;
  582. buf_size1 -= S5P_FIMV_ENC_UPMV_SIZE;
  583. mfc_write(dev, OFFSETA(buf_addr1),
  584. S5P_FIMV_MPEG4_COZERO_FLAG_ADR);
  585. buf_addr1 += S5P_FIMV_ENC_COLFLG_SIZE;
  586. buf_size1 -= S5P_FIMV_ENC_COLFLG_SIZE;
  587. mfc_write(dev, OFFSETA(buf_addr1),
  588. S5P_FIMV_MPEG4_ACDC_COEF_ADR);
  589. buf_addr1 += S5P_FIMV_ENC_ACDCCOEF_SIZE;
  590. buf_size1 -= S5P_FIMV_ENC_ACDCCOEF_SIZE;
  591. mfc_debug(2, "buf_size1: %zu, buf_size2: %zu\n",
  592. buf_size1, buf_size2);
  593. break;
  594. case S5P_MFC_CODEC_H263_ENC:
  595. for (i = 0; i < 2; i++) {
  596. mfc_write(dev, OFFSETA(buf_addr1),
  597. S5P_FIMV_ENC_REF0_LUMA_ADR + (4 * i));
  598. buf_addr1 += enc_ref_y_size;
  599. buf_size1 -= enc_ref_y_size;
  600. mfc_write(dev, OFFSETB(buf_addr2),
  601. S5P_FIMV_ENC_REF2_LUMA_ADR + (4 * i));
  602. buf_addr2 += enc_ref_y_size;
  603. buf_size2 -= enc_ref_y_size;
  604. }
  605. for (i = 0; i < 4; i++) {
  606. mfc_write(dev, OFFSETB(buf_addr2),
  607. S5P_FIMV_ENC_REF0_CHROMA_ADR + (4 * i));
  608. buf_addr2 += enc_ref_c_size;
  609. buf_size2 -= enc_ref_c_size;
  610. }
  611. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H263_UP_MV_ADR);
  612. buf_addr1 += S5P_FIMV_ENC_UPMV_SIZE;
  613. buf_size1 -= S5P_FIMV_ENC_UPMV_SIZE;
  614. mfc_write(dev, OFFSETA(buf_addr1), S5P_FIMV_H263_ACDC_COEF_ADR);
  615. buf_addr1 += S5P_FIMV_ENC_ACDCCOEF_SIZE;
  616. buf_size1 -= S5P_FIMV_ENC_ACDCCOEF_SIZE;
  617. mfc_debug(2, "buf_size1: %zu, buf_size2: %zu\n",
  618. buf_size1, buf_size2);
  619. break;
  620. default:
  621. mfc_err("Unknown codec set for encoding: %d\n",
  622. ctx->codec_mode);
  623. return -EINVAL;
  624. }
  625. return 0;
  626. }
  627. static int s5p_mfc_set_enc_params(struct s5p_mfc_ctx *ctx)
  628. {
  629. struct s5p_mfc_dev *dev = ctx->dev;
  630. struct s5p_mfc_enc_params *p = &ctx->enc_params;
  631. unsigned int reg;
  632. unsigned int shm;
  633. /* width */
  634. mfc_write(dev, ctx->img_width, S5P_FIMV_ENC_HSIZE_PX);
  635. /* height */
  636. mfc_write(dev, ctx->img_height, S5P_FIMV_ENC_VSIZE_PX);
  637. /* pictype : enable, IDR period */
  638. reg = mfc_read(dev, S5P_FIMV_ENC_PIC_TYPE_CTRL);
  639. reg |= (1 << 18);
  640. reg &= ~(0xFFFF);
  641. reg |= p->gop_size;
  642. mfc_write(dev, reg, S5P_FIMV_ENC_PIC_TYPE_CTRL);
  643. mfc_write(dev, 0, S5P_FIMV_ENC_B_RECON_WRITE_ON);
  644. /* multi-slice control */
  645. /* multi-slice MB number or bit size */
  646. mfc_write(dev, p->slice_mode, S5P_FIMV_ENC_MSLICE_CTRL);
  647. if (p->slice_mode == V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB) {
  648. mfc_write(dev, p->slice_mb, S5P_FIMV_ENC_MSLICE_MB);
  649. } else if (p->slice_mode == V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES) {
  650. mfc_write(dev, p->slice_bit, S5P_FIMV_ENC_MSLICE_BIT);
  651. } else {
  652. mfc_write(dev, 0, S5P_FIMV_ENC_MSLICE_MB);
  653. mfc_write(dev, 0, S5P_FIMV_ENC_MSLICE_BIT);
  654. }
  655. /* cyclic intra refresh */
  656. mfc_write(dev, p->intra_refresh_mb, S5P_FIMV_ENC_CIR_CTRL);
  657. /* memory structure cur. frame */
  658. if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12M)
  659. mfc_write(dev, 0, S5P_FIMV_ENC_MAP_FOR_CUR);
  660. else if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12MT)
  661. mfc_write(dev, 3, S5P_FIMV_ENC_MAP_FOR_CUR);
  662. /* padding control & value */
  663. reg = mfc_read(dev, S5P_FIMV_ENC_PADDING_CTRL);
  664. if (p->pad) {
  665. /** enable */
  666. reg |= (1 << 31);
  667. /** cr value */
  668. reg &= ~(0xFF << 16);
  669. reg |= (p->pad_cr << 16);
  670. /** cb value */
  671. reg &= ~(0xFF << 8);
  672. reg |= (p->pad_cb << 8);
  673. /** y value */
  674. reg &= ~(0xFF);
  675. reg |= (p->pad_luma);
  676. } else {
  677. /** disable & all value clear */
  678. reg = 0;
  679. }
  680. mfc_write(dev, reg, S5P_FIMV_ENC_PADDING_CTRL);
  681. /* rate control config. */
  682. reg = mfc_read(dev, S5P_FIMV_ENC_RC_CONFIG);
  683. /** frame-level rate control */
  684. reg &= ~(0x1 << 9);
  685. reg |= (p->rc_frame << 9);
  686. mfc_write(dev, reg, S5P_FIMV_ENC_RC_CONFIG);
  687. /* bit rate */
  688. if (p->rc_frame)
  689. mfc_write(dev, p->rc_bitrate,
  690. S5P_FIMV_ENC_RC_BIT_RATE);
  691. else
  692. mfc_write(dev, 0, S5P_FIMV_ENC_RC_BIT_RATE);
  693. /* reaction coefficient */
  694. if (p->rc_frame)
  695. mfc_write(dev, p->rc_reaction_coeff, S5P_FIMV_ENC_RC_RPARA);
  696. shm = s5p_mfc_read_info_v5(ctx, EXT_ENC_CONTROL);
  697. /* seq header ctrl */
  698. shm &= ~(0x1 << 3);
  699. shm |= (p->seq_hdr_mode << 3);
  700. /* frame skip mode */
  701. shm &= ~(0x3 << 1);
  702. shm |= (p->frame_skip_mode << 1);
  703. s5p_mfc_write_info_v5(ctx, shm, EXT_ENC_CONTROL);
  704. /* fixed target bit */
  705. s5p_mfc_write_info_v5(ctx, p->fixed_target_bit, RC_CONTROL_CONFIG);
  706. return 0;
  707. }
  708. static int s5p_mfc_set_enc_params_h264(struct s5p_mfc_ctx *ctx)
  709. {
  710. struct s5p_mfc_dev *dev = ctx->dev;
  711. struct s5p_mfc_enc_params *p = &ctx->enc_params;
  712. struct s5p_mfc_h264_enc_params *p_264 = &p->codec.h264;
  713. unsigned int reg;
  714. unsigned int shm;
  715. s5p_mfc_set_enc_params(ctx);
  716. /* pictype : number of B */
  717. reg = mfc_read(dev, S5P_FIMV_ENC_PIC_TYPE_CTRL);
  718. /* num_b_frame - 0 ~ 2 */
  719. reg &= ~(0x3 << 16);
  720. reg |= (p->num_b_frame << 16);
  721. mfc_write(dev, reg, S5P_FIMV_ENC_PIC_TYPE_CTRL);
  722. /* profile & level */
  723. reg = mfc_read(dev, S5P_FIMV_ENC_PROFILE);
  724. /* level */
  725. reg &= ~(0xFF << 8);
  726. reg |= (p_264->level << 8);
  727. /* profile - 0 ~ 2 */
  728. reg &= ~(0x3F);
  729. reg |= p_264->profile;
  730. mfc_write(dev, reg, S5P_FIMV_ENC_PROFILE);
  731. /* interlace */
  732. mfc_write(dev, p_264->interlace, S5P_FIMV_ENC_PIC_STRUCT);
  733. /* height */
  734. if (p_264->interlace)
  735. mfc_write(dev, ctx->img_height >> 1, S5P_FIMV_ENC_VSIZE_PX);
  736. /* loopfilter ctrl */
  737. mfc_write(dev, p_264->loop_filter_mode, S5P_FIMV_ENC_LF_CTRL);
  738. /* loopfilter alpha offset */
  739. if (p_264->loop_filter_alpha < 0) {
  740. reg = 0x10;
  741. reg |= (0xFF - p_264->loop_filter_alpha) + 1;
  742. } else {
  743. reg = 0x00;
  744. reg |= (p_264->loop_filter_alpha & 0xF);
  745. }
  746. mfc_write(dev, reg, S5P_FIMV_ENC_ALPHA_OFF);
  747. /* loopfilter beta offset */
  748. if (p_264->loop_filter_beta < 0) {
  749. reg = 0x10;
  750. reg |= (0xFF - p_264->loop_filter_beta) + 1;
  751. } else {
  752. reg = 0x00;
  753. reg |= (p_264->loop_filter_beta & 0xF);
  754. }
  755. mfc_write(dev, reg, S5P_FIMV_ENC_BETA_OFF);
  756. /* entropy coding mode */
  757. if (p_264->entropy_mode == V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC)
  758. mfc_write(dev, 1, S5P_FIMV_ENC_H264_ENTROPY_MODE);
  759. else
  760. mfc_write(dev, 0, S5P_FIMV_ENC_H264_ENTROPY_MODE);
  761. /* number of ref. picture */
  762. reg = mfc_read(dev, S5P_FIMV_ENC_H264_NUM_OF_REF);
  763. /* num of ref. pictures of P */
  764. reg &= ~(0x3 << 5);
  765. reg |= (p_264->num_ref_pic_4p << 5);
  766. /* max number of ref. pictures */
  767. reg &= ~(0x1F);
  768. reg |= p_264->max_ref_pic;
  769. mfc_write(dev, reg, S5P_FIMV_ENC_H264_NUM_OF_REF);
  770. /* 8x8 transform enable */
  771. mfc_write(dev, p_264->_8x8_transform, S5P_FIMV_ENC_H264_TRANS_FLAG);
  772. /* rate control config. */
  773. reg = mfc_read(dev, S5P_FIMV_ENC_RC_CONFIG);
  774. /* macroblock level rate control */
  775. reg &= ~(0x1 << 8);
  776. reg |= (p->rc_mb << 8);
  777. /* frame QP */
  778. reg &= ~(0x3F);
  779. reg |= p_264->rc_frame_qp;
  780. mfc_write(dev, reg, S5P_FIMV_ENC_RC_CONFIG);
  781. /* frame rate */
  782. if (p->rc_frame && p->rc_framerate_denom)
  783. mfc_write(dev, p->rc_framerate_num * 1000
  784. / p->rc_framerate_denom, S5P_FIMV_ENC_RC_FRAME_RATE);
  785. else
  786. mfc_write(dev, 0, S5P_FIMV_ENC_RC_FRAME_RATE);
  787. /* max & min value of QP */
  788. reg = mfc_read(dev, S5P_FIMV_ENC_RC_QBOUND);
  789. /* max QP */
  790. reg &= ~(0x3F << 8);
  791. reg |= (p_264->rc_max_qp << 8);
  792. /* min QP */
  793. reg &= ~(0x3F);
  794. reg |= p_264->rc_min_qp;
  795. mfc_write(dev, reg, S5P_FIMV_ENC_RC_QBOUND);
  796. /* macroblock adaptive scaling features */
  797. if (p->rc_mb) {
  798. reg = mfc_read(dev, S5P_FIMV_ENC_RC_MB_CTRL);
  799. /* dark region */
  800. reg &= ~(0x1 << 3);
  801. reg |= (p_264->rc_mb_dark << 3);
  802. /* smooth region */
  803. reg &= ~(0x1 << 2);
  804. reg |= (p_264->rc_mb_smooth << 2);
  805. /* static region */
  806. reg &= ~(0x1 << 1);
  807. reg |= (p_264->rc_mb_static << 1);
  808. /* high activity region */
  809. reg &= ~(0x1);
  810. reg |= p_264->rc_mb_activity;
  811. mfc_write(dev, reg, S5P_FIMV_ENC_RC_MB_CTRL);
  812. }
  813. if (!p->rc_frame && !p->rc_mb) {
  814. shm = s5p_mfc_read_info_v5(ctx, P_B_FRAME_QP);
  815. shm &= ~(0xFFF);
  816. shm |= ((p_264->rc_b_frame_qp & 0x3F) << 6);
  817. shm |= (p_264->rc_p_frame_qp & 0x3F);
  818. s5p_mfc_write_info_v5(ctx, shm, P_B_FRAME_QP);
  819. }
  820. /* extended encoder ctrl */
  821. shm = s5p_mfc_read_info_v5(ctx, EXT_ENC_CONTROL);
  822. /* AR VUI control */
  823. shm &= ~(0x1 << 15);
  824. shm |= (p_264->vui_sar << 1);
  825. s5p_mfc_write_info_v5(ctx, shm, EXT_ENC_CONTROL);
  826. if (p_264->vui_sar) {
  827. /* aspect ration IDC */
  828. shm = s5p_mfc_read_info_v5(ctx, SAMPLE_ASPECT_RATIO_IDC);
  829. shm &= ~(0xFF);
  830. shm |= p_264->vui_sar_idc;
  831. s5p_mfc_write_info_v5(ctx, shm, SAMPLE_ASPECT_RATIO_IDC);
  832. if (p_264->vui_sar_idc == 0xFF) {
  833. /* sample AR info */
  834. shm = s5p_mfc_read_info_v5(ctx, EXTENDED_SAR);
  835. shm &= ~(0xFFFFFFFF);
  836. shm |= p_264->vui_ext_sar_width << 16;
  837. shm |= p_264->vui_ext_sar_height;
  838. s5p_mfc_write_info_v5(ctx, shm, EXTENDED_SAR);
  839. }
  840. }
  841. /* intra picture period for H.264 */
  842. shm = s5p_mfc_read_info_v5(ctx, H264_I_PERIOD);
  843. /* control */
  844. shm &= ~(0x1 << 16);
  845. shm |= (p_264->open_gop << 16);
  846. /* value */
  847. if (p_264->open_gop) {
  848. shm &= ~(0xFFFF);
  849. shm |= p_264->open_gop_size;
  850. }
  851. s5p_mfc_write_info_v5(ctx, shm, H264_I_PERIOD);
  852. /* extended encoder ctrl */
  853. shm = s5p_mfc_read_info_v5(ctx, EXT_ENC_CONTROL);
  854. /* vbv buffer size */
  855. if (p->frame_skip_mode ==
  856. V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT) {
  857. shm &= ~(0xFFFF << 16);
  858. shm |= (p_264->cpb_size << 16);
  859. }
  860. s5p_mfc_write_info_v5(ctx, shm, EXT_ENC_CONTROL);
  861. return 0;
  862. }
  863. static int s5p_mfc_set_enc_params_mpeg4(struct s5p_mfc_ctx *ctx)
  864. {
  865. struct s5p_mfc_dev *dev = ctx->dev;
  866. struct s5p_mfc_enc_params *p = &ctx->enc_params;
  867. struct s5p_mfc_mpeg4_enc_params *p_mpeg4 = &p->codec.mpeg4;
  868. unsigned int reg;
  869. unsigned int shm;
  870. unsigned int framerate;
  871. s5p_mfc_set_enc_params(ctx);
  872. /* pictype : number of B */
  873. reg = mfc_read(dev, S5P_FIMV_ENC_PIC_TYPE_CTRL);
  874. /* num_b_frame - 0 ~ 2 */
  875. reg &= ~(0x3 << 16);
  876. reg |= (p->num_b_frame << 16);
  877. mfc_write(dev, reg, S5P_FIMV_ENC_PIC_TYPE_CTRL);
  878. /* profile & level */
  879. reg = mfc_read(dev, S5P_FIMV_ENC_PROFILE);
  880. /* level */
  881. reg &= ~(0xFF << 8);
  882. reg |= (p_mpeg4->level << 8);
  883. /* profile - 0 ~ 2 */
  884. reg &= ~(0x3F);
  885. reg |= p_mpeg4->profile;
  886. mfc_write(dev, reg, S5P_FIMV_ENC_PROFILE);
  887. /* quarter_pixel */
  888. mfc_write(dev, p_mpeg4->quarter_pixel, S5P_FIMV_ENC_MPEG4_QUART_PXL);
  889. /* qp */
  890. if (!p->rc_frame) {
  891. shm = s5p_mfc_read_info_v5(ctx, P_B_FRAME_QP);
  892. shm &= ~(0xFFF);
  893. shm |= ((p_mpeg4->rc_b_frame_qp & 0x3F) << 6);
  894. shm |= (p_mpeg4->rc_p_frame_qp & 0x3F);
  895. s5p_mfc_write_info_v5(ctx, shm, P_B_FRAME_QP);
  896. }
  897. /* frame rate */
  898. if (p->rc_frame) {
  899. if (p->rc_framerate_denom > 0) {
  900. framerate = p->rc_framerate_num * 1000 /
  901. p->rc_framerate_denom;
  902. mfc_write(dev, framerate,
  903. S5P_FIMV_ENC_RC_FRAME_RATE);
  904. shm = s5p_mfc_read_info_v5(ctx, RC_VOP_TIMING);
  905. shm &= ~(0xFFFFFFFF);
  906. shm |= (1 << 31);
  907. shm |= ((p->rc_framerate_num & 0x7FFF) << 16);
  908. shm |= (p->rc_framerate_denom & 0xFFFF);
  909. s5p_mfc_write_info_v5(ctx, shm, RC_VOP_TIMING);
  910. }
  911. } else {
  912. mfc_write(dev, 0, S5P_FIMV_ENC_RC_FRAME_RATE);
  913. }
  914. /* rate control config. */
  915. reg = mfc_read(dev, S5P_FIMV_ENC_RC_CONFIG);
  916. /* frame QP */
  917. reg &= ~(0x3F);
  918. reg |= p_mpeg4->rc_frame_qp;
  919. mfc_write(dev, reg, S5P_FIMV_ENC_RC_CONFIG);
  920. /* max & min value of QP */
  921. reg = mfc_read(dev, S5P_FIMV_ENC_RC_QBOUND);
  922. /* max QP */
  923. reg &= ~(0x3F << 8);
  924. reg |= (p_mpeg4->rc_max_qp << 8);
  925. /* min QP */
  926. reg &= ~(0x3F);
  927. reg |= p_mpeg4->rc_min_qp;
  928. mfc_write(dev, reg, S5P_FIMV_ENC_RC_QBOUND);
  929. /* extended encoder ctrl */
  930. shm = s5p_mfc_read_info_v5(ctx, EXT_ENC_CONTROL);
  931. /* vbv buffer size */
  932. if (p->frame_skip_mode ==
  933. V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT) {
  934. shm &= ~(0xFFFF << 16);
  935. shm |= (p->vbv_size << 16);
  936. }
  937. s5p_mfc_write_info_v5(ctx, shm, EXT_ENC_CONTROL);
  938. return 0;
  939. }
  940. static int s5p_mfc_set_enc_params_h263(struct s5p_mfc_ctx *ctx)
  941. {
  942. struct s5p_mfc_dev *dev = ctx->dev;
  943. struct s5p_mfc_enc_params *p = &ctx->enc_params;
  944. struct s5p_mfc_mpeg4_enc_params *p_h263 = &p->codec.mpeg4;
  945. unsigned int reg;
  946. unsigned int shm;
  947. s5p_mfc_set_enc_params(ctx);
  948. /* qp */
  949. if (!p->rc_frame) {
  950. shm = s5p_mfc_read_info_v5(ctx, P_B_FRAME_QP);
  951. shm &= ~(0xFFF);
  952. shm |= (p_h263->rc_p_frame_qp & 0x3F);
  953. s5p_mfc_write_info_v5(ctx, shm, P_B_FRAME_QP);
  954. }
  955. /* frame rate */
  956. if (p->rc_frame && p->rc_framerate_denom)
  957. mfc_write(dev, p->rc_framerate_num * 1000
  958. / p->rc_framerate_denom, S5P_FIMV_ENC_RC_FRAME_RATE);
  959. else
  960. mfc_write(dev, 0, S5P_FIMV_ENC_RC_FRAME_RATE);
  961. /* rate control config. */
  962. reg = mfc_read(dev, S5P_FIMV_ENC_RC_CONFIG);
  963. /* frame QP */
  964. reg &= ~(0x3F);
  965. reg |= p_h263->rc_frame_qp;
  966. mfc_write(dev, reg, S5P_FIMV_ENC_RC_CONFIG);
  967. /* max & min value of QP */
  968. reg = mfc_read(dev, S5P_FIMV_ENC_RC_QBOUND);
  969. /* max QP */
  970. reg &= ~(0x3F << 8);
  971. reg |= (p_h263->rc_max_qp << 8);
  972. /* min QP */
  973. reg &= ~(0x3F);
  974. reg |= p_h263->rc_min_qp;
  975. mfc_write(dev, reg, S5P_FIMV_ENC_RC_QBOUND);
  976. /* extended encoder ctrl */
  977. shm = s5p_mfc_read_info_v5(ctx, EXT_ENC_CONTROL);
  978. /* vbv buffer size */
  979. if (p->frame_skip_mode ==
  980. V4L2_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT) {
  981. shm &= ~(0xFFFF << 16);
  982. shm |= (p->vbv_size << 16);
  983. }
  984. s5p_mfc_write_info_v5(ctx, shm, EXT_ENC_CONTROL);
  985. return 0;
  986. }
  987. /* Initialize decoding */
  988. static int s5p_mfc_init_decode_v5(struct s5p_mfc_ctx *ctx)
  989. {
  990. struct s5p_mfc_dev *dev = ctx->dev;
  991. s5p_mfc_set_shared_buffer(ctx);
  992. /* Setup loop filter, for decoding this is only valid for MPEG4 */
  993. if (ctx->codec_mode == S5P_MFC_CODEC_MPEG4_DEC)
  994. mfc_write(dev, ctx->loop_filter_mpeg4, S5P_FIMV_ENC_LF_CTRL);
  995. else
  996. mfc_write(dev, 0, S5P_FIMV_ENC_LF_CTRL);
  997. mfc_write(dev, ((ctx->slice_interface & S5P_FIMV_SLICE_INT_MASK) <<
  998. S5P_FIMV_SLICE_INT_SHIFT) | (ctx->display_delay_enable <<
  999. S5P_FIMV_DDELAY_ENA_SHIFT) | ((ctx->display_delay &
  1000. S5P_FIMV_DDELAY_VAL_MASK) << S5P_FIMV_DDELAY_VAL_SHIFT),
  1001. S5P_FIMV_SI_CH0_DPB_CONF_CTRL);
  1002. mfc_write(dev,
  1003. ((S5P_FIMV_CH_SEQ_HEADER & S5P_FIMV_CH_MASK) << S5P_FIMV_CH_SHIFT)
  1004. | (ctx->inst_no), S5P_FIMV_SI_CH0_INST_ID);
  1005. return 0;
  1006. }
  1007. static void s5p_mfc_set_flush(struct s5p_mfc_ctx *ctx, int flush)
  1008. {
  1009. struct s5p_mfc_dev *dev = ctx->dev;
  1010. unsigned int dpb;
  1011. if (flush)
  1012. dpb = mfc_read(dev, S5P_FIMV_SI_CH0_DPB_CONF_CTRL) | (
  1013. S5P_FIMV_DPB_FLUSH_MASK << S5P_FIMV_DPB_FLUSH_SHIFT);
  1014. else
  1015. dpb = mfc_read(dev, S5P_FIMV_SI_CH0_DPB_CONF_CTRL) &
  1016. ~(S5P_FIMV_DPB_FLUSH_MASK << S5P_FIMV_DPB_FLUSH_SHIFT);
  1017. mfc_write(dev, dpb, S5P_FIMV_SI_CH0_DPB_CONF_CTRL);
  1018. }
  1019. /* Decode a single frame */
  1020. static int s5p_mfc_decode_one_frame_v5(struct s5p_mfc_ctx *ctx,
  1021. enum s5p_mfc_decode_arg last_frame)
  1022. {
  1023. struct s5p_mfc_dev *dev = ctx->dev;
  1024. mfc_write(dev, ctx->dec_dst_flag, S5P_FIMV_SI_CH0_RELEASE_BUF);
  1025. s5p_mfc_set_shared_buffer(ctx);
  1026. s5p_mfc_set_flush(ctx, ctx->dpb_flush_flag);
  1027. /* Issue different commands to instance basing on whether it
  1028. * is the last frame or not. */
  1029. switch (last_frame) {
  1030. case MFC_DEC_FRAME:
  1031. mfc_write(dev, ((S5P_FIMV_CH_FRAME_START & S5P_FIMV_CH_MASK) <<
  1032. S5P_FIMV_CH_SHIFT) | (ctx->inst_no), S5P_FIMV_SI_CH0_INST_ID);
  1033. break;
  1034. case MFC_DEC_LAST_FRAME:
  1035. mfc_write(dev, ((S5P_FIMV_CH_LAST_FRAME & S5P_FIMV_CH_MASK) <<
  1036. S5P_FIMV_CH_SHIFT) | (ctx->inst_no), S5P_FIMV_SI_CH0_INST_ID);
  1037. break;
  1038. case MFC_DEC_RES_CHANGE:
  1039. mfc_write(dev, ((S5P_FIMV_CH_FRAME_START_REALLOC &
  1040. S5P_FIMV_CH_MASK) << S5P_FIMV_CH_SHIFT) | (ctx->inst_no),
  1041. S5P_FIMV_SI_CH0_INST_ID);
  1042. break;
  1043. }
  1044. mfc_debug(2, "Decoding a usual frame\n");
  1045. return 0;
  1046. }
  1047. static int s5p_mfc_init_encode_v5(struct s5p_mfc_ctx *ctx)
  1048. {
  1049. struct s5p_mfc_dev *dev = ctx->dev;
  1050. if (ctx->codec_mode == S5P_MFC_CODEC_H264_ENC)
  1051. s5p_mfc_set_enc_params_h264(ctx);
  1052. else if (ctx->codec_mode == S5P_MFC_CODEC_MPEG4_ENC)
  1053. s5p_mfc_set_enc_params_mpeg4(ctx);
  1054. else if (ctx->codec_mode == S5P_MFC_CODEC_H263_ENC)
  1055. s5p_mfc_set_enc_params_h263(ctx);
  1056. else {
  1057. mfc_err("Unknown codec for encoding (%x)\n",
  1058. ctx->codec_mode);
  1059. return -EINVAL;
  1060. }
  1061. s5p_mfc_set_shared_buffer(ctx);
  1062. mfc_write(dev, ((S5P_FIMV_CH_SEQ_HEADER << 16) & 0x70000) |
  1063. (ctx->inst_no), S5P_FIMV_SI_CH0_INST_ID);
  1064. return 0;
  1065. }
  1066. /* Encode a single frame */
  1067. static int s5p_mfc_encode_one_frame_v5(struct s5p_mfc_ctx *ctx)
  1068. {
  1069. struct s5p_mfc_dev *dev = ctx->dev;
  1070. int cmd;
  1071. /* memory structure cur. frame */
  1072. if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12M)
  1073. mfc_write(dev, 0, S5P_FIMV_ENC_MAP_FOR_CUR);
  1074. else if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_NV12MT)
  1075. mfc_write(dev, 3, S5P_FIMV_ENC_MAP_FOR_CUR);
  1076. s5p_mfc_set_shared_buffer(ctx);
  1077. if (ctx->state == MFCINST_FINISHING)
  1078. cmd = S5P_FIMV_CH_LAST_FRAME;
  1079. else
  1080. cmd = S5P_FIMV_CH_FRAME_START;
  1081. mfc_write(dev, ((cmd & S5P_FIMV_CH_MASK) << S5P_FIMV_CH_SHIFT)
  1082. | (ctx->inst_no), S5P_FIMV_SI_CH0_INST_ID);
  1083. return 0;
  1084. }
  1085. static void s5p_mfc_run_res_change(struct s5p_mfc_ctx *ctx)
  1086. {
  1087. struct s5p_mfc_dev *dev = ctx->dev;
  1088. s5p_mfc_set_dec_stream_buffer_v5(ctx, 0, 0, 0);
  1089. dev->curr_ctx = ctx->num;
  1090. s5p_mfc_decode_one_frame_v5(ctx, MFC_DEC_RES_CHANGE);
  1091. }
  1092. static int s5p_mfc_run_dec_frame(struct s5p_mfc_ctx *ctx, int last_frame)
  1093. {
  1094. struct s5p_mfc_dev *dev = ctx->dev;
  1095. struct s5p_mfc_buf *temp_vb;
  1096. if (ctx->state == MFCINST_FINISHING) {
  1097. last_frame = MFC_DEC_LAST_FRAME;
  1098. s5p_mfc_set_dec_stream_buffer_v5(ctx, 0, 0, 0);
  1099. dev->curr_ctx = ctx->num;
  1100. s5p_mfc_decode_one_frame_v5(ctx, last_frame);
  1101. return 0;
  1102. }
  1103. /* Frames are being decoded */
  1104. if (list_empty(&ctx->src_queue)) {
  1105. mfc_debug(2, "No src buffers\n");
  1106. return -EAGAIN;
  1107. }
  1108. /* Get the next source buffer */
  1109. temp_vb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
  1110. temp_vb->flags |= MFC_BUF_FLAG_USED;
  1111. s5p_mfc_set_dec_stream_buffer_v5(ctx,
  1112. vb2_dma_contig_plane_dma_addr(&temp_vb->b->vb2_buf, 0),
  1113. ctx->consumed_stream, temp_vb->b->vb2_buf.planes[0].bytesused);
  1114. dev->curr_ctx = ctx->num;
  1115. if (temp_vb->b->vb2_buf.planes[0].bytesused == 0) {
  1116. last_frame = MFC_DEC_LAST_FRAME;
  1117. mfc_debug(2, "Setting ctx->state to FINISHING\n");
  1118. ctx->state = MFCINST_FINISHING;
  1119. }
  1120. s5p_mfc_decode_one_frame_v5(ctx, last_frame);
  1121. return 0;
  1122. }
  1123. static int s5p_mfc_run_enc_frame(struct s5p_mfc_ctx *ctx)
  1124. {
  1125. struct s5p_mfc_dev *dev = ctx->dev;
  1126. struct s5p_mfc_buf *dst_mb;
  1127. struct s5p_mfc_buf *src_mb;
  1128. unsigned long src_y_addr, src_c_addr, dst_addr;
  1129. unsigned int dst_size;
  1130. if (list_empty(&ctx->src_queue) && ctx->state != MFCINST_FINISHING) {
  1131. mfc_debug(2, "no src buffers\n");
  1132. return -EAGAIN;
  1133. }
  1134. if (list_empty(&ctx->dst_queue)) {
  1135. mfc_debug(2, "no dst buffers\n");
  1136. return -EAGAIN;
  1137. }
  1138. if (list_empty(&ctx->src_queue)) {
  1139. /* send null frame */
  1140. s5p_mfc_set_enc_frame_buffer_v5(ctx, dev->bank2, dev->bank2);
  1141. src_mb = NULL;
  1142. } else {
  1143. src_mb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
  1144. list);
  1145. src_mb->flags |= MFC_BUF_FLAG_USED;
  1146. if (src_mb->b->vb2_buf.planes[0].bytesused == 0) {
  1147. /* send null frame */
  1148. s5p_mfc_set_enc_frame_buffer_v5(ctx, dev->bank2,
  1149. dev->bank2);
  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. }