vpu_malone.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2020-2021 NXP
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/init.h>
  7. #include <linux/interconnect.h>
  8. #include <linux/ioctl.h>
  9. #include <linux/list.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/delay.h>
  14. #include <linux/rational.h>
  15. #include <linux/time64.h>
  16. #include <media/videobuf2-v4l2.h>
  17. #include <media/videobuf2-dma-contig.h>
  18. #include <linux/videodev2.h>
  19. #include "vpu.h"
  20. #include "vpu_rpc.h"
  21. #include "vpu_defs.h"
  22. #include "vpu_helpers.h"
  23. #include "vpu_v4l2.h"
  24. #include "vpu_cmds.h"
  25. #include "vpu_imx8q.h"
  26. #include "vpu_malone.h"
  27. static bool low_latency;
  28. module_param(low_latency, bool, 0644);
  29. MODULE_PARM_DESC(low_latency, "Set low latency frame flush mode: 0 (disable) or 1 (enable)");
  30. #define CMD_SIZE 25600
  31. #define MSG_SIZE 25600
  32. #define CODEC_SIZE 0x1000
  33. #define JPEG_SIZE 0x1000
  34. #define SEQ_SIZE 0x1000
  35. #define GOP_SIZE 0x1000
  36. #define PIC_SIZE 0x1000
  37. #define QMETER_SIZE 0x1000
  38. #define DBGLOG_SIZE 0x10000
  39. #define DEBUG_SIZE 0x80000
  40. #define ENG_SIZE 0x1000
  41. #define MALONE_SKIPPED_FRAME_ID 0x555
  42. #define MALONE_ALIGN_MBI 0x800
  43. #define MALONE_DCP_CHUNK_BIT 16
  44. #define MALONE_DCP_SIZE_MAX 0x3000000
  45. #define MALONE_DCP_SIZE_MIN 0x100000
  46. #define MALONE_DCP_FIXED_MB_ALLOC 250
  47. #define CONFIG_SET(val, cfg, pos, mask) \
  48. (*(cfg) |= (((val) << (pos)) & (mask)))
  49. //x means source data , y means destination data
  50. #define STREAM_CONFIG_FORMAT_SET(x, y) CONFIG_SET(x, y, 0, 0x0000000F)
  51. #define STREAM_CONFIG_STRBUFIDX_SET(x, y) CONFIG_SET(x, y, 8, 0x00000300)
  52. #define STREAM_CONFIG_NOSEQ_SET(x, y) CONFIG_SET(x, y, 10, 0x00000400)
  53. #define STREAM_CONFIG_DEBLOCK_SET(x, y) CONFIG_SET(x, y, 11, 0x00000800)
  54. #define STREAM_CONFIG_DERING_SET(x, y) CONFIG_SET(x, y, 12, 0x00001000)
  55. #define STREAM_CONFIG_IBWAIT_SET(x, y) CONFIG_SET(x, y, 13, 0x00002000)
  56. #define STREAM_CONFIG_FBC_SET(x, y) CONFIG_SET(x, y, 14, 0x00004000)
  57. #define STREAM_CONFIG_PLAY_MODE_SET(x, y) CONFIG_SET(x, y, 16, 0x00030000)
  58. #define STREAM_CONFIG_ENABLE_DCP_SET(x, y) CONFIG_SET(x, y, 20, 0x00100000)
  59. #define STREAM_CONFIG_NUM_STR_BUF_SET(x, y) CONFIG_SET(x, y, 21, 0x00600000)
  60. #define STREAM_CONFIG_MALONE_USAGE_SET(x, y) CONFIG_SET(x, y, 23, 0x01800000)
  61. #define STREAM_CONFIG_MULTI_VID_SET(x, y) CONFIG_SET(x, y, 25, 0x02000000)
  62. #define STREAM_CONFIG_OBFUSC_EN_SET(x, y) CONFIG_SET(x, y, 26, 0x04000000)
  63. #define STREAM_CONFIG_RC4_EN_SET(x, y) CONFIG_SET(x, y, 27, 0x08000000)
  64. #define STREAM_CONFIG_MCX_SET(x, y) CONFIG_SET(x, y, 28, 0x10000000)
  65. #define STREAM_CONFIG_PES_SET(x, y) CONFIG_SET(x, y, 29, 0x20000000)
  66. #define STREAM_CONFIG_NUM_DBE_SET(x, y) CONFIG_SET(x, y, 30, 0x40000000)
  67. #define STREAM_CONFIG_FS_CTRL_MODE_SET(x, y) CONFIG_SET(x, y, 31, 0x80000000)
  68. #define MALONE_DEC_FMT_RV_MASK BIT(21)
  69. #define MALONE_VERSION_MASK 0xFFFFF
  70. #define MALONE_VERSION(maj, min, inc) \
  71. (FIELD_PREP(0xF0000, maj) | FIELD_PREP(0xFF00, min) | FIELD_PREP(0xFF, inc))
  72. #define CHECK_VERSION(iface, maj, min) \
  73. (FIELD_GET(MALONE_VERSION_MASK, (iface)->fw_version) >= MALONE_VERSION(maj, min, 0))
  74. enum vpu_malone_stream_input_mode {
  75. INVALID_MODE = 0,
  76. FRAME_LVL,
  77. NON_FRAME_LVL
  78. };
  79. enum vpu_malone_format {
  80. MALONE_FMT_NULL = 0x0,
  81. MALONE_FMT_AVC = 0x1,
  82. MALONE_FMT_MP2 = 0x2,
  83. MALONE_FMT_VC1 = 0x3,
  84. MALONE_FMT_AVS = 0x4,
  85. MALONE_FMT_ASP = 0x5,
  86. MALONE_FMT_JPG = 0x6,
  87. MALONE_FMT_RV = 0x7,
  88. MALONE_FMT_VP6 = 0x8,
  89. MALONE_FMT_SPK = 0x9,
  90. MALONE_FMT_VP8 = 0xA,
  91. MALONE_FMT_HEVC = 0xB,
  92. MALONE_FMT_LAST = MALONE_FMT_HEVC
  93. };
  94. enum {
  95. VID_API_CMD_NULL = 0x00,
  96. VID_API_CMD_PARSE_NEXT_SEQ = 0x01,
  97. VID_API_CMD_PARSE_NEXT_I = 0x02,
  98. VID_API_CMD_PARSE_NEXT_IP = 0x03,
  99. VID_API_CMD_PARSE_NEXT_ANY = 0x04,
  100. VID_API_CMD_DEC_PIC = 0x05,
  101. VID_API_CMD_UPDATE_ES_WR_PTR = 0x06,
  102. VID_API_CMD_UPDATE_ES_RD_PTR = 0x07,
  103. VID_API_CMD_UPDATE_UDATA = 0x08,
  104. VID_API_CMD_GET_FSINFO = 0x09,
  105. VID_API_CMD_SKIP_PIC = 0x0a,
  106. VID_API_CMD_DEC_CHUNK = 0x0b,
  107. VID_API_CMD_START = 0x10,
  108. VID_API_CMD_STOP = 0x11,
  109. VID_API_CMD_ABORT = 0x12,
  110. VID_API_CMD_RST_BUF = 0x13,
  111. VID_API_CMD_FS_RELEASE = 0x15,
  112. VID_API_CMD_MEM_REGION_ATTACH = 0x16,
  113. VID_API_CMD_MEM_REGION_DETACH = 0x17,
  114. VID_API_CMD_MVC_VIEW_SELECT = 0x18,
  115. VID_API_CMD_FS_ALLOC = 0x19,
  116. VID_API_CMD_DBG_GET_STATUS = 0x1C,
  117. VID_API_CMD_DBG_START_LOG = 0x1D,
  118. VID_API_CMD_DBG_STOP_LOG = 0x1E,
  119. VID_API_CMD_DBG_DUMP_LOG = 0x1F,
  120. VID_API_CMD_YUV_READY = 0x20,
  121. VID_API_CMD_TS = 0x21,
  122. VID_API_CMD_FIRM_RESET = 0x40,
  123. VID_API_CMD_SNAPSHOT = 0xAA,
  124. VID_API_CMD_ROLL_SNAPSHOT = 0xAB,
  125. VID_API_CMD_LOCK_SCHEDULER = 0xAC,
  126. VID_API_CMD_UNLOCK_SCHEDULER = 0xAD,
  127. VID_API_CMD_CQ_FIFO_DUMP = 0xAE,
  128. VID_API_CMD_DBG_FIFO_DUMP = 0xAF,
  129. VID_API_CMD_SVC_ILP = 0xBB,
  130. VID_API_CMD_FW_STATUS = 0xF0,
  131. VID_API_CMD_INVALID = 0xFF
  132. };
  133. enum {
  134. VID_API_EVENT_NULL = 0x00,
  135. VID_API_EVENT_RESET_DONE = 0x01,
  136. VID_API_EVENT_SEQ_HDR_FOUND = 0x02,
  137. VID_API_EVENT_PIC_HDR_FOUND = 0x03,
  138. VID_API_EVENT_PIC_DECODED = 0x04,
  139. VID_API_EVENT_FIFO_LOW = 0x05,
  140. VID_API_EVENT_FIFO_HIGH = 0x06,
  141. VID_API_EVENT_FIFO_EMPTY = 0x07,
  142. VID_API_EVENT_FIFO_FULL = 0x08,
  143. VID_API_EVENT_BS_ERROR = 0x09,
  144. VID_API_EVENT_UDATA_FIFO_UPTD = 0x0A,
  145. VID_API_EVENT_RES_CHANGE = 0x0B,
  146. VID_API_EVENT_FIFO_OVF = 0x0C,
  147. VID_API_EVENT_CHUNK_DECODED = 0x0D,
  148. VID_API_EVENT_REQ_FRAME_BUFF = 0x10,
  149. VID_API_EVENT_FRAME_BUFF_RDY = 0x11,
  150. VID_API_EVENT_REL_FRAME_BUFF = 0x12,
  151. VID_API_EVENT_STR_BUF_RST = 0x13,
  152. VID_API_EVENT_RET_PING = 0x14,
  153. VID_API_EVENT_QMETER = 0x15,
  154. VID_API_EVENT_STR_FMT_CHANGE = 0x16,
  155. VID_API_EVENT_FIRMWARE_XCPT = 0x17,
  156. VID_API_EVENT_START_DONE = 0x18,
  157. VID_API_EVENT_STOPPED = 0x19,
  158. VID_API_EVENT_ABORT_DONE = 0x1A,
  159. VID_API_EVENT_FINISHED = 0x1B,
  160. VID_API_EVENT_DBG_STAT_UPDATE = 0x1C,
  161. VID_API_EVENT_DBG_LOG_STARTED = 0x1D,
  162. VID_API_EVENT_DBG_LOG_STOPPED = 0x1E,
  163. VID_API_EVENT_DBG_LOG_UPDATED = 0x1F,
  164. VID_API_EVENT_DBG_MSG_DEC = 0x20,
  165. VID_API_EVENT_DEC_SC_ERR = 0x21,
  166. VID_API_EVENT_CQ_FIFO_DUMP = 0x22,
  167. VID_API_EVENT_DBG_FIFO_DUMP = 0x23,
  168. VID_API_EVENT_DEC_CHECK_RES = 0x24,
  169. VID_API_EVENT_DEC_CFG_INFO = 0x25,
  170. VID_API_EVENT_UNSUPPORTED_STREAM = 0x26,
  171. VID_API_EVENT_PIC_SKIPPED = 0x27,
  172. VID_API_EVENT_STR_SUSPENDED = 0x30,
  173. VID_API_EVENT_SNAPSHOT_DONE = 0x40,
  174. VID_API_EVENT_FW_STATUS = 0xF0,
  175. VID_API_EVENT_INVALID = 0xFF
  176. };
  177. struct vpu_malone_buffer_desc {
  178. struct vpu_rpc_buffer_desc buffer;
  179. u32 low;
  180. u32 high;
  181. };
  182. struct vpu_malone_str_buffer {
  183. u32 wptr;
  184. u32 rptr;
  185. u32 start;
  186. u32 end;
  187. u32 lwm;
  188. };
  189. struct vpu_malone_picth_info {
  190. u32 frame_pitch;
  191. };
  192. struct vpu_malone_table_desc {
  193. u32 array_base;
  194. u32 size;
  195. };
  196. struct vpu_malone_dbglog_desc {
  197. u32 addr;
  198. u32 size;
  199. u32 level;
  200. u32 reserved;
  201. };
  202. struct vpu_malone_udata {
  203. u32 base;
  204. u32 total_size;
  205. u32 slot_size;
  206. };
  207. struct vpu_malone_buffer_info {
  208. u32 stream_input_mode;
  209. u32 stream_pic_input_count;
  210. u32 stream_pic_parsed_count;
  211. u32 stream_buffer_threshold;
  212. u32 stream_pic_end_flag;
  213. };
  214. struct vpu_malone_encrypt_info {
  215. u32 rec4key[8];
  216. u32 obfusc;
  217. };
  218. struct malone_iface {
  219. u32 exec_base_addr;
  220. u32 exec_area_size;
  221. struct vpu_malone_buffer_desc cmd_buffer_desc;
  222. struct vpu_malone_buffer_desc msg_buffer_desc;
  223. u32 cmd_int_enable[VID_API_NUM_STREAMS];
  224. struct vpu_malone_picth_info stream_pitch_info[VID_API_NUM_STREAMS];
  225. u32 stream_config[VID_API_NUM_STREAMS];
  226. struct vpu_malone_table_desc codec_param_tab_desc;
  227. struct vpu_malone_table_desc jpeg_param_tab_desc;
  228. u32 stream_buffer_desc[VID_API_NUM_STREAMS][VID_API_MAX_BUF_PER_STR];
  229. struct vpu_malone_table_desc seq_info_tab_desc;
  230. struct vpu_malone_table_desc pic_info_tab_desc;
  231. struct vpu_malone_table_desc gop_info_tab_desc;
  232. struct vpu_malone_table_desc qmeter_info_tab_desc;
  233. u32 stream_error[VID_API_NUM_STREAMS];
  234. u32 fw_version;
  235. u32 fw_offset;
  236. u32 max_streams;
  237. struct vpu_malone_dbglog_desc dbglog_desc;
  238. struct vpu_rpc_buffer_desc api_cmd_buffer_desc[VID_API_NUM_STREAMS];
  239. struct vpu_malone_udata udata_buffer[VID_API_NUM_STREAMS];
  240. struct vpu_malone_buffer_desc debug_buffer_desc;
  241. struct vpu_malone_buffer_desc eng_access_buff_desc[VID_API_NUM_STREAMS];
  242. u32 encrypt_info[VID_API_NUM_STREAMS];
  243. struct vpu_rpc_system_config system_cfg;
  244. u32 api_version;
  245. struct vpu_malone_buffer_info stream_buff_info[VID_API_NUM_STREAMS];
  246. };
  247. struct malone_jpg_params {
  248. u32 rotation_angle;
  249. u32 horiz_scale_factor;
  250. u32 vert_scale_factor;
  251. u32 rotation_mode;
  252. u32 rgb_mode;
  253. u32 chunk_mode; /* 0 ~ 1 */
  254. u32 last_chunk; /* 0 ~ 1 */
  255. u32 chunk_rows; /* 0 ~ 255 */
  256. u32 num_bytes;
  257. u32 jpg_crop_x;
  258. u32 jpg_crop_y;
  259. u32 jpg_crop_width;
  260. u32 jpg_crop_height;
  261. u32 jpg_mjpeg_mode;
  262. u32 jpg_mjpeg_interlaced;
  263. };
  264. struct malone_codec_params {
  265. u32 disp_imm;
  266. u32 fourcc;
  267. u32 codec_version;
  268. u32 frame_rate;
  269. u32 dbglog_enable;
  270. u32 bsdma_lwm;
  271. u32 bbd_coring;
  272. u32 bbd_s_thr_row;
  273. u32 bbd_p_thr_row;
  274. u32 bbd_s_thr_logo_row;
  275. u32 bbd_p_thr_logo_row;
  276. u32 bbd_s_thr_col;
  277. u32 bbd_p_thr_col;
  278. u32 bbd_chr_thr_row;
  279. u32 bbd_chr_thr_col;
  280. u32 bbd_uv_mid_level;
  281. u32 bbd_excl_win_mb_left;
  282. u32 bbd_excl_win_mb_right;
  283. };
  284. struct malone_padding_scode {
  285. u32 scode_type;
  286. u32 pixelformat;
  287. u32 data[2];
  288. };
  289. struct malone_fmt_mapping {
  290. u32 pixelformat;
  291. enum vpu_malone_format malone_format;
  292. u32 is_disabled;
  293. };
  294. struct malone_scode_t {
  295. struct vpu_inst *inst;
  296. struct vb2_buffer *vb;
  297. u32 wptr;
  298. u32 need_data;
  299. };
  300. struct malone_scode_handler {
  301. u32 pixelformat;
  302. int (*insert_scode_seq)(struct malone_scode_t *scode);
  303. int (*insert_scode_pic)(struct malone_scode_t *scode);
  304. };
  305. struct vpu_dec_ctrl {
  306. struct malone_codec_params *codec_param;
  307. struct malone_jpg_params *jpg;
  308. void *seq_mem;
  309. void *pic_mem;
  310. void *gop_mem;
  311. void *qmeter_mem;
  312. void *dbglog_mem;
  313. struct vpu_malone_str_buffer __iomem *str_buf[VID_API_NUM_STREAMS];
  314. u32 buf_addr[VID_API_NUM_STREAMS];
  315. };
  316. static const struct malone_padding_scode *get_padding_scode(u32 type, u32 fmt);
  317. u32 vpu_malone_get_data_size(void)
  318. {
  319. return sizeof(struct vpu_dec_ctrl);
  320. }
  321. void vpu_malone_init_rpc(struct vpu_shared_addr *shared,
  322. struct vpu_buffer *rpc, dma_addr_t boot_addr)
  323. {
  324. struct malone_iface *iface;
  325. struct vpu_dec_ctrl *hc;
  326. unsigned long base_phy_addr;
  327. unsigned long phy_addr;
  328. unsigned long offset;
  329. unsigned int i;
  330. if (rpc->phys < boot_addr)
  331. return;
  332. iface = rpc->virt;
  333. base_phy_addr = rpc->phys - boot_addr;
  334. hc = shared->priv;
  335. shared->iface = iface;
  336. shared->boot_addr = boot_addr;
  337. iface->exec_base_addr = base_phy_addr;
  338. iface->exec_area_size = rpc->length;
  339. offset = sizeof(struct malone_iface);
  340. phy_addr = base_phy_addr + offset;
  341. shared->cmd_desc = &iface->cmd_buffer_desc.buffer;
  342. shared->cmd_mem_vir = rpc->virt + offset;
  343. iface->cmd_buffer_desc.buffer.start =
  344. iface->cmd_buffer_desc.buffer.rptr =
  345. iface->cmd_buffer_desc.buffer.wptr = phy_addr;
  346. iface->cmd_buffer_desc.buffer.end = iface->cmd_buffer_desc.buffer.start + CMD_SIZE;
  347. offset += CMD_SIZE;
  348. phy_addr = base_phy_addr + offset;
  349. shared->msg_desc = &iface->msg_buffer_desc.buffer;
  350. shared->msg_mem_vir = rpc->virt + offset;
  351. iface->msg_buffer_desc.buffer.start =
  352. iface->msg_buffer_desc.buffer.wptr =
  353. iface->msg_buffer_desc.buffer.rptr = phy_addr;
  354. iface->msg_buffer_desc.buffer.end = iface->msg_buffer_desc.buffer.start + MSG_SIZE;
  355. offset += MSG_SIZE;
  356. phy_addr = base_phy_addr + offset;
  357. iface->codec_param_tab_desc.array_base = phy_addr;
  358. hc->codec_param = rpc->virt + offset;
  359. offset += CODEC_SIZE;
  360. phy_addr = base_phy_addr + offset;
  361. iface->jpeg_param_tab_desc.array_base = phy_addr;
  362. hc->jpg = rpc->virt + offset;
  363. offset += JPEG_SIZE;
  364. phy_addr = base_phy_addr + offset;
  365. iface->seq_info_tab_desc.array_base = phy_addr;
  366. hc->seq_mem = rpc->virt + offset;
  367. offset += SEQ_SIZE;
  368. phy_addr = base_phy_addr + offset;
  369. iface->pic_info_tab_desc.array_base = phy_addr;
  370. hc->pic_mem = rpc->virt + offset;
  371. offset += PIC_SIZE;
  372. phy_addr = base_phy_addr + offset;
  373. iface->gop_info_tab_desc.array_base = phy_addr;
  374. hc->gop_mem = rpc->virt + offset;
  375. offset += GOP_SIZE;
  376. phy_addr = base_phy_addr + offset;
  377. iface->qmeter_info_tab_desc.array_base = phy_addr;
  378. hc->qmeter_mem = rpc->virt + offset;
  379. offset += QMETER_SIZE;
  380. phy_addr = base_phy_addr + offset;
  381. iface->dbglog_desc.addr = phy_addr;
  382. iface->dbglog_desc.size = DBGLOG_SIZE;
  383. hc->dbglog_mem = rpc->virt + offset;
  384. offset += DBGLOG_SIZE;
  385. phy_addr = base_phy_addr + offset;
  386. for (i = 0; i < VID_API_NUM_STREAMS; i++) {
  387. iface->eng_access_buff_desc[i].buffer.start =
  388. iface->eng_access_buff_desc[i].buffer.wptr =
  389. iface->eng_access_buff_desc[i].buffer.rptr = phy_addr;
  390. iface->eng_access_buff_desc[i].buffer.end =
  391. iface->eng_access_buff_desc[i].buffer.start + ENG_SIZE;
  392. offset += ENG_SIZE;
  393. phy_addr = base_phy_addr + offset;
  394. }
  395. for (i = 0; i < VID_API_NUM_STREAMS; i++) {
  396. iface->encrypt_info[i] = phy_addr;
  397. offset += sizeof(struct vpu_malone_encrypt_info);
  398. phy_addr = base_phy_addr + offset;
  399. }
  400. rpc->bytesused = offset;
  401. }
  402. void vpu_malone_set_log_buf(struct vpu_shared_addr *shared,
  403. struct vpu_buffer *log)
  404. {
  405. struct malone_iface *iface = shared->iface;
  406. iface->debug_buffer_desc.buffer.start =
  407. iface->debug_buffer_desc.buffer.wptr =
  408. iface->debug_buffer_desc.buffer.rptr = log->phys - shared->boot_addr;
  409. iface->debug_buffer_desc.buffer.end = iface->debug_buffer_desc.buffer.start + log->length;
  410. }
  411. static u32 get_str_buffer_offset(u32 instance)
  412. {
  413. return DEC_MFD_XREG_SLV_BASE + MFD_MCX + MFD_MCX_OFF * instance;
  414. }
  415. void vpu_malone_set_system_cfg(struct vpu_shared_addr *shared,
  416. u32 regs_base, void __iomem *regs, u32 core_id)
  417. {
  418. struct malone_iface *iface = shared->iface;
  419. struct vpu_rpc_system_config *config = &iface->system_cfg;
  420. struct vpu_dec_ctrl *hc = shared->priv;
  421. int i;
  422. vpu_imx8q_set_system_cfg_common(config, regs_base, core_id);
  423. for (i = 0; i < VID_API_NUM_STREAMS; i++) {
  424. u32 offset = get_str_buffer_offset(i);
  425. hc->buf_addr[i] = regs_base + offset;
  426. hc->str_buf[i] = regs + offset;
  427. }
  428. }
  429. u32 vpu_malone_get_version(struct vpu_shared_addr *shared)
  430. {
  431. struct malone_iface *iface = shared->iface;
  432. vpu_malone_enable_format(V4L2_PIX_FMT_RV30, iface->fw_version & MALONE_DEC_FMT_RV_MASK);
  433. vpu_malone_enable_format(V4L2_PIX_FMT_RV40, iface->fw_version & MALONE_DEC_FMT_RV_MASK);
  434. return iface->fw_version;
  435. }
  436. int vpu_malone_get_stream_buffer_size(struct vpu_shared_addr *shared)
  437. {
  438. return 0xc00000;
  439. }
  440. int vpu_malone_config_stream_buffer(struct vpu_shared_addr *shared,
  441. u32 instance,
  442. struct vpu_buffer *buf)
  443. {
  444. struct malone_iface *iface = shared->iface;
  445. struct vpu_dec_ctrl *hc = shared->priv;
  446. struct vpu_malone_str_buffer __iomem *str_buf = hc->str_buf[instance];
  447. writel(buf->phys, &str_buf->start);
  448. writel(buf->phys, &str_buf->rptr);
  449. writel(buf->phys, &str_buf->wptr);
  450. writel(buf->phys + buf->length, &str_buf->end);
  451. writel(0x1, &str_buf->lwm);
  452. iface->stream_buffer_desc[instance][0] = hc->buf_addr[instance];
  453. return 0;
  454. }
  455. int vpu_malone_get_stream_buffer_desc(struct vpu_shared_addr *shared,
  456. u32 instance,
  457. struct vpu_rpc_buffer_desc *desc)
  458. {
  459. struct vpu_dec_ctrl *hc = shared->priv;
  460. struct vpu_malone_str_buffer __iomem *str_buf = hc->str_buf[instance];
  461. if (desc) {
  462. desc->wptr = readl(&str_buf->wptr);
  463. desc->rptr = readl(&str_buf->rptr);
  464. desc->start = readl(&str_buf->start);
  465. desc->end = readl(&str_buf->end);
  466. }
  467. return 0;
  468. }
  469. static void vpu_malone_update_wptr(struct vpu_malone_str_buffer __iomem *str_buf, u32 wptr)
  470. {
  471. /*update wptr after data is written*/
  472. mb();
  473. writel(wptr, &str_buf->wptr);
  474. }
  475. static void vpu_malone_update_rptr(struct vpu_malone_str_buffer __iomem *str_buf, u32 rptr)
  476. {
  477. /*update rptr after data is read*/
  478. mb();
  479. writel(rptr, &str_buf->rptr);
  480. }
  481. int vpu_malone_update_stream_buffer(struct vpu_shared_addr *shared,
  482. u32 instance, u32 ptr, bool write)
  483. {
  484. struct vpu_dec_ctrl *hc = shared->priv;
  485. struct vpu_malone_str_buffer __iomem *str_buf = hc->str_buf[instance];
  486. if (write)
  487. vpu_malone_update_wptr(str_buf, ptr);
  488. else
  489. vpu_malone_update_rptr(str_buf, ptr);
  490. return 0;
  491. }
  492. static struct malone_fmt_mapping fmt_mappings[] = {
  493. {V4L2_PIX_FMT_H264, MALONE_FMT_AVC},
  494. {V4L2_PIX_FMT_H264_MVC, MALONE_FMT_AVC},
  495. {V4L2_PIX_FMT_HEVC, MALONE_FMT_HEVC},
  496. {V4L2_PIX_FMT_VC1_ANNEX_G, MALONE_FMT_VC1},
  497. {V4L2_PIX_FMT_VC1_ANNEX_L, MALONE_FMT_VC1},
  498. {V4L2_PIX_FMT_MPEG2, MALONE_FMT_MP2},
  499. {V4L2_PIX_FMT_MPEG4, MALONE_FMT_ASP},
  500. {V4L2_PIX_FMT_XVID, MALONE_FMT_ASP},
  501. {V4L2_PIX_FMT_H263, MALONE_FMT_ASP},
  502. {V4L2_PIX_FMT_JPEG, MALONE_FMT_JPG},
  503. {V4L2_PIX_FMT_VP8, MALONE_FMT_VP8},
  504. {V4L2_PIX_FMT_SPK, MALONE_FMT_SPK},
  505. {V4L2_PIX_FMT_RV30, MALONE_FMT_RV},
  506. {V4L2_PIX_FMT_RV40, MALONE_FMT_RV},
  507. };
  508. void vpu_malone_enable_format(u32 pixelformat, int enable)
  509. {
  510. u32 i;
  511. for (i = 0; i < ARRAY_SIZE(fmt_mappings); i++) {
  512. if (pixelformat == fmt_mappings[i].pixelformat) {
  513. fmt_mappings[i].is_disabled = enable ? 0 : 1;
  514. return;
  515. }
  516. }
  517. }
  518. static enum vpu_malone_format vpu_malone_format_remap(u32 pixelformat)
  519. {
  520. u32 i;
  521. for (i = 0; i < ARRAY_SIZE(fmt_mappings); i++) {
  522. if (fmt_mappings[i].is_disabled)
  523. continue;
  524. if (pixelformat == fmt_mappings[i].pixelformat)
  525. return fmt_mappings[i].malone_format;
  526. }
  527. return MALONE_FMT_NULL;
  528. }
  529. bool vpu_malone_check_fmt(enum vpu_core_type type, u32 pixelfmt)
  530. {
  531. if (!vpu_imx8q_check_fmt(type, pixelfmt))
  532. return false;
  533. if (pixelfmt == V4L2_PIX_FMT_NV12_8L128 || pixelfmt == V4L2_PIX_FMT_NV12_10BE_8L128 ||
  534. pixelfmt == V4L2_PIX_FMT_NV12M_8L128 || pixelfmt == V4L2_PIX_FMT_NV12M_10BE_8L128)
  535. return true;
  536. if (vpu_malone_format_remap(pixelfmt) == MALONE_FMT_NULL)
  537. return false;
  538. return true;
  539. }
  540. static void vpu_malone_set_stream_cfg(struct vpu_shared_addr *shared,
  541. u32 instance,
  542. enum vpu_malone_format malone_format)
  543. {
  544. struct malone_iface *iface = shared->iface;
  545. u32 *curr_str_cfg = &iface->stream_config[instance];
  546. *curr_str_cfg = 0;
  547. STREAM_CONFIG_FORMAT_SET(malone_format, curr_str_cfg);
  548. STREAM_CONFIG_STRBUFIDX_SET(0, curr_str_cfg);
  549. STREAM_CONFIG_NOSEQ_SET(0, curr_str_cfg);
  550. STREAM_CONFIG_DEBLOCK_SET(0, curr_str_cfg);
  551. STREAM_CONFIG_DERING_SET(0, curr_str_cfg);
  552. STREAM_CONFIG_PLAY_MODE_SET(0x3, curr_str_cfg);
  553. STREAM_CONFIG_FS_CTRL_MODE_SET(0x1, curr_str_cfg);
  554. STREAM_CONFIG_ENABLE_DCP_SET(1, curr_str_cfg);
  555. STREAM_CONFIG_NUM_STR_BUF_SET(1, curr_str_cfg);
  556. STREAM_CONFIG_MALONE_USAGE_SET(1, curr_str_cfg);
  557. STREAM_CONFIG_MULTI_VID_SET(0, curr_str_cfg);
  558. STREAM_CONFIG_OBFUSC_EN_SET(0, curr_str_cfg);
  559. STREAM_CONFIG_RC4_EN_SET(0, curr_str_cfg);
  560. STREAM_CONFIG_MCX_SET(1, curr_str_cfg);
  561. STREAM_CONFIG_PES_SET(0, curr_str_cfg);
  562. STREAM_CONFIG_NUM_DBE_SET(1, curr_str_cfg);
  563. }
  564. static int vpu_malone_set_params(struct vpu_shared_addr *shared,
  565. u32 instance,
  566. struct vpu_decode_params *params)
  567. {
  568. struct malone_iface *iface = shared->iface;
  569. struct vpu_dec_ctrl *hc = shared->priv;
  570. enum vpu_malone_format malone_format;
  571. malone_format = vpu_malone_format_remap(params->codec_format);
  572. if (WARN_ON(malone_format == MALONE_FMT_NULL))
  573. return -EINVAL;
  574. iface->udata_buffer[instance].base = params->udata.base;
  575. iface->udata_buffer[instance].slot_size = params->udata.size;
  576. vpu_malone_set_stream_cfg(shared, instance, malone_format);
  577. if (malone_format == MALONE_FMT_JPG) {
  578. //1:JPGD_MJPEG_MODE_A; 2:JPGD_MJPEG_MODE_B
  579. hc->jpg[instance].jpg_mjpeg_mode = 1;
  580. //0: JPGD_MJPEG_PROGRESSIVE
  581. hc->jpg[instance].jpg_mjpeg_interlaced = 0;
  582. }
  583. if (params->display_delay_enable &&
  584. get_padding_scode(SCODE_PADDING_BUFFLUSH, params->codec_format))
  585. hc->codec_param[instance].disp_imm = 1;
  586. else
  587. hc->codec_param[instance].disp_imm = 0;
  588. if (params->codec_format == V4L2_PIX_FMT_HEVC && !CHECK_VERSION(iface, 1, 9))
  589. hc->codec_param[instance].disp_imm = 0;
  590. hc->codec_param[instance].dbglog_enable = 0;
  591. iface->dbglog_desc.level = 0;
  592. if (params->b_non_frame)
  593. iface->stream_buff_info[instance].stream_input_mode = NON_FRAME_LVL;
  594. else
  595. iface->stream_buff_info[instance].stream_input_mode = FRAME_LVL;
  596. iface->stream_buff_info[instance].stream_buffer_threshold = 0;
  597. iface->stream_buff_info[instance].stream_pic_input_count = 0;
  598. return 0;
  599. }
  600. static bool vpu_malone_is_non_frame_mode(struct vpu_shared_addr *shared, u32 instance)
  601. {
  602. struct malone_iface *iface = shared->iface;
  603. if (iface->stream_buff_info[instance].stream_input_mode == NON_FRAME_LVL)
  604. return true;
  605. return false;
  606. }
  607. static int vpu_malone_update_params(struct vpu_shared_addr *shared,
  608. u32 instance,
  609. struct vpu_decode_params *params)
  610. {
  611. struct malone_iface *iface = shared->iface;
  612. if (params->end_flag)
  613. iface->stream_buff_info[instance].stream_pic_end_flag = params->end_flag;
  614. params->end_flag = 0;
  615. return 0;
  616. }
  617. int vpu_malone_set_decode_params(struct vpu_shared_addr *shared,
  618. u32 instance,
  619. struct vpu_decode_params *params,
  620. u32 update)
  621. {
  622. if (!params)
  623. return -EINVAL;
  624. if (!update)
  625. return vpu_malone_set_params(shared, instance, params);
  626. else
  627. return vpu_malone_update_params(shared, instance, params);
  628. }
  629. static struct vpu_pair malone_cmds[] = {
  630. {VPU_CMD_ID_NOOP, VID_API_CMD_NULL},
  631. {VPU_CMD_ID_START, VID_API_CMD_START},
  632. {VPU_CMD_ID_STOP, VID_API_CMD_STOP},
  633. {VPU_CMD_ID_ABORT, VID_API_CMD_ABORT},
  634. {VPU_CMD_ID_RST_BUF, VID_API_CMD_RST_BUF},
  635. {VPU_CMD_ID_SNAPSHOT, VID_API_CMD_SNAPSHOT},
  636. {VPU_CMD_ID_FIRM_RESET, VID_API_CMD_FIRM_RESET},
  637. {VPU_CMD_ID_FS_ALLOC, VID_API_CMD_FS_ALLOC},
  638. {VPU_CMD_ID_FS_RELEASE, VID_API_CMD_FS_RELEASE},
  639. {VPU_CMD_ID_TIMESTAMP, VID_API_CMD_TS},
  640. {VPU_CMD_ID_DEBUG, VID_API_CMD_FW_STATUS},
  641. };
  642. static struct vpu_pair malone_msgs[] = {
  643. {VPU_MSG_ID_RESET_DONE, VID_API_EVENT_RESET_DONE},
  644. {VPU_MSG_ID_START_DONE, VID_API_EVENT_START_DONE},
  645. {VPU_MSG_ID_STOP_DONE, VID_API_EVENT_STOPPED},
  646. {VPU_MSG_ID_ABORT_DONE, VID_API_EVENT_ABORT_DONE},
  647. {VPU_MSG_ID_BUF_RST, VID_API_EVENT_STR_BUF_RST},
  648. {VPU_MSG_ID_PIC_EOS, VID_API_EVENT_FINISHED},
  649. {VPU_MSG_ID_SEQ_HDR_FOUND, VID_API_EVENT_SEQ_HDR_FOUND},
  650. {VPU_MSG_ID_RES_CHANGE, VID_API_EVENT_RES_CHANGE},
  651. {VPU_MSG_ID_PIC_HDR_FOUND, VID_API_EVENT_PIC_HDR_FOUND},
  652. {VPU_MSG_ID_PIC_DECODED, VID_API_EVENT_PIC_DECODED},
  653. {VPU_MSG_ID_DEC_DONE, VID_API_EVENT_FRAME_BUFF_RDY},
  654. {VPU_MSG_ID_FRAME_REQ, VID_API_EVENT_REQ_FRAME_BUFF},
  655. {VPU_MSG_ID_FRAME_RELEASE, VID_API_EVENT_REL_FRAME_BUFF},
  656. {VPU_MSG_ID_FIFO_LOW, VID_API_EVENT_FIFO_LOW},
  657. {VPU_MSG_ID_BS_ERROR, VID_API_EVENT_BS_ERROR},
  658. {VPU_MSG_ID_UNSUPPORTED, VID_API_EVENT_UNSUPPORTED_STREAM},
  659. {VPU_MSG_ID_FIRMWARE_XCPT, VID_API_EVENT_FIRMWARE_XCPT},
  660. {VPU_MSG_ID_PIC_SKIPPED, VID_API_EVENT_PIC_SKIPPED},
  661. {VPU_MSG_ID_DBG_MSG, VID_API_EVENT_DBG_MSG_DEC},
  662. };
  663. static void vpu_malone_pack_fs_alloc(struct vpu_rpc_event *pkt,
  664. struct vpu_fs_info *fs)
  665. {
  666. const u32 fs_type[] = {
  667. [MEM_RES_FRAME] = 0,
  668. [MEM_RES_MBI] = 1,
  669. [MEM_RES_DCP] = 2,
  670. };
  671. pkt->hdr.num = 7;
  672. pkt->data[0] = fs->id | (fs->tag << 24);
  673. pkt->data[1] = fs->luma_addr;
  674. if (fs->type == MEM_RES_FRAME) {
  675. /*
  676. * if luma_addr equal to chroma_addr,
  677. * means luma(plane[0]) and chromau(plane[1]) used the
  678. * same fd -- usage of NXP codec2. Need to manually
  679. * offset chroma addr.
  680. */
  681. if (fs->luma_addr == fs->chroma_addr)
  682. fs->chroma_addr = fs->luma_addr + fs->luma_size;
  683. pkt->data[2] = fs->luma_addr + fs->luma_size / 2;
  684. pkt->data[3] = fs->chroma_addr;
  685. pkt->data[4] = fs->chroma_addr + fs->chromau_size / 2;
  686. pkt->data[5] = fs->bytesperline;
  687. } else {
  688. pkt->data[2] = fs->luma_size;
  689. pkt->data[3] = 0;
  690. pkt->data[4] = 0;
  691. pkt->data[5] = 0;
  692. }
  693. pkt->data[6] = fs_type[fs->type];
  694. }
  695. static void vpu_malone_pack_fs_release(struct vpu_rpc_event *pkt,
  696. struct vpu_fs_info *fs)
  697. {
  698. pkt->hdr.num = 1;
  699. pkt->data[0] = fs->id | (fs->tag << 24);
  700. }
  701. static void vpu_malone_pack_timestamp(struct vpu_rpc_event *pkt,
  702. struct vpu_ts_info *info)
  703. {
  704. struct timespec64 ts = ns_to_timespec64(info->timestamp);
  705. pkt->hdr.num = 3;
  706. pkt->data[0] = ts.tv_sec;
  707. pkt->data[1] = ts.tv_nsec;
  708. pkt->data[2] = info->size;
  709. }
  710. int vpu_malone_pack_cmd(struct vpu_rpc_event *pkt, u32 index, u32 id, void *data)
  711. {
  712. int ret;
  713. ret = vpu_find_dst_by_src(malone_cmds, ARRAY_SIZE(malone_cmds), id);
  714. if (ret < 0)
  715. return ret;
  716. pkt->hdr.id = ret;
  717. pkt->hdr.num = 0;
  718. pkt->hdr.index = index;
  719. switch (id) {
  720. case VPU_CMD_ID_FS_ALLOC:
  721. vpu_malone_pack_fs_alloc(pkt, data);
  722. break;
  723. case VPU_CMD_ID_FS_RELEASE:
  724. vpu_malone_pack_fs_release(pkt, data);
  725. break;
  726. case VPU_CMD_ID_TIMESTAMP:
  727. vpu_malone_pack_timestamp(pkt, data);
  728. break;
  729. }
  730. pkt->hdr.index = index;
  731. return 0;
  732. }
  733. int vpu_malone_convert_msg_id(u32 id)
  734. {
  735. return vpu_find_src_by_dst(malone_msgs, ARRAY_SIZE(malone_msgs), id);
  736. }
  737. static void vpu_malone_fill_planes(struct vpu_dec_codec_info *info)
  738. {
  739. u32 interlaced = info->progressive ? 0 : 1;
  740. info->bytesperline[0] = 0;
  741. info->sizeimage[0] = vpu_helper_get_plane_size(info->pixfmt,
  742. info->decoded_width,
  743. info->decoded_height,
  744. 0,
  745. info->stride,
  746. interlaced,
  747. &info->bytesperline[0]);
  748. info->bytesperline[1] = 0;
  749. info->sizeimage[1] = vpu_helper_get_plane_size(info->pixfmt,
  750. info->decoded_width,
  751. info->decoded_height,
  752. 1,
  753. info->stride,
  754. interlaced,
  755. &info->bytesperline[1]);
  756. }
  757. static void vpu_malone_init_seq_hdr(struct vpu_dec_codec_info *info)
  758. {
  759. u32 chunks = info->num_dfe_area >> MALONE_DCP_CHUNK_BIT;
  760. vpu_malone_fill_planes(info);
  761. info->mbi_size = (info->sizeimage[0] + info->sizeimage[1]) >> 2;
  762. info->mbi_size = ALIGN(info->mbi_size, MALONE_ALIGN_MBI);
  763. info->dcp_size = MALONE_DCP_SIZE_MAX;
  764. if (chunks) {
  765. u32 mb_num;
  766. u32 mb_w;
  767. u32 mb_h;
  768. mb_w = DIV_ROUND_UP(info->decoded_width, 16);
  769. mb_h = DIV_ROUND_UP(info->decoded_height, 16);
  770. mb_num = mb_w * mb_h;
  771. info->dcp_size = mb_num * MALONE_DCP_FIXED_MB_ALLOC * chunks;
  772. info->dcp_size = clamp_t(u32, info->dcp_size,
  773. MALONE_DCP_SIZE_MIN, MALONE_DCP_SIZE_MAX);
  774. }
  775. }
  776. static void vpu_malone_unpack_seq_hdr(struct vpu_rpc_event *pkt,
  777. struct vpu_dec_codec_info *info)
  778. {
  779. info->num_ref_frms = pkt->data[0];
  780. info->num_dpb_frms = pkt->data[1];
  781. info->num_dfe_area = pkt->data[2];
  782. info->progressive = pkt->data[3];
  783. info->width = pkt->data[5];
  784. info->height = pkt->data[4];
  785. info->decoded_width = pkt->data[12];
  786. info->decoded_height = pkt->data[11];
  787. info->frame_rate.numerator = 1000;
  788. info->frame_rate.denominator = pkt->data[8];
  789. info->dsp_asp_ratio = pkt->data[9];
  790. info->profile_idc = (pkt->data[10] >> 8) & 0xff;
  791. info->level_idc = pkt->data[10] & 0xff;
  792. info->bit_depth_luma = pkt->data[13];
  793. info->bit_depth_chroma = pkt->data[14];
  794. info->chroma_fmt = pkt->data[15];
  795. info->color_primaries = vpu_color_cvrt_primaries_i2v(pkt->data[16]);
  796. info->transfer_chars = vpu_color_cvrt_transfers_i2v(pkt->data[17]);
  797. info->matrix_coeffs = vpu_color_cvrt_matrix_i2v(pkt->data[18]);
  798. info->full_range = vpu_color_cvrt_full_range_i2v(pkt->data[19]);
  799. info->vui_present = pkt->data[20];
  800. info->mvc_num_views = pkt->data[21];
  801. info->offset_x = pkt->data[23];
  802. info->offset_y = pkt->data[25];
  803. info->tag = pkt->data[27];
  804. if (info->bit_depth_luma > 8)
  805. info->pixfmt = V4L2_PIX_FMT_NV12M_10BE_8L128;
  806. else
  807. info->pixfmt = V4L2_PIX_FMT_NV12M_8L128;
  808. if (pkt->hdr.num > 28)
  809. info->constraint_set_flags = pkt->data[28];
  810. if (info->frame_rate.numerator && info->frame_rate.denominator) {
  811. unsigned long n, d;
  812. rational_best_approximation(info->frame_rate.numerator,
  813. info->frame_rate.denominator,
  814. info->frame_rate.numerator,
  815. info->frame_rate.denominator,
  816. &n, &d);
  817. info->frame_rate.numerator = n;
  818. info->frame_rate.denominator = d;
  819. }
  820. vpu_malone_init_seq_hdr(info);
  821. }
  822. static void vpu_malone_unpack_pic_info(struct vpu_rpc_event *pkt,
  823. struct vpu_dec_pic_info *info)
  824. {
  825. info->id = pkt->data[7];
  826. info->luma = pkt->data[0];
  827. info->start = pkt->data[10];
  828. info->end = pkt->data[12];
  829. info->pic_size = pkt->data[11];
  830. info->stride = pkt->data[5];
  831. info->consumed_count = pkt->data[13];
  832. if (info->id == MALONE_SKIPPED_FRAME_ID)
  833. info->skipped = 1;
  834. else
  835. info->skipped = 0;
  836. }
  837. static void vpu_malone_unpack_req_frame(struct vpu_rpc_event *pkt,
  838. struct vpu_fs_info *info)
  839. {
  840. info->type = pkt->data[1];
  841. }
  842. static void vpu_malone_unpack_rel_frame(struct vpu_rpc_event *pkt,
  843. struct vpu_fs_info *info)
  844. {
  845. info->id = pkt->data[0];
  846. info->type = pkt->data[1];
  847. info->not_displayed = pkt->data[2];
  848. }
  849. static void vpu_malone_unpack_buff_rdy(struct vpu_rpc_event *pkt,
  850. struct vpu_dec_pic_info *info)
  851. {
  852. struct timespec64 ts = { pkt->data[9], pkt->data[10] };
  853. info->id = pkt->data[0];
  854. info->luma = pkt->data[1];
  855. info->stride = pkt->data[3];
  856. if (info->id == MALONE_SKIPPED_FRAME_ID)
  857. info->skipped = 1;
  858. else
  859. info->skipped = 0;
  860. info->timestamp = timespec64_to_ns(&ts);
  861. }
  862. int vpu_malone_unpack_msg_data(struct vpu_rpc_event *pkt, void *data)
  863. {
  864. if (!pkt || !data)
  865. return -EINVAL;
  866. switch (pkt->hdr.id) {
  867. case VID_API_EVENT_SEQ_HDR_FOUND:
  868. vpu_malone_unpack_seq_hdr(pkt, data);
  869. break;
  870. case VID_API_EVENT_PIC_DECODED:
  871. vpu_malone_unpack_pic_info(pkt, data);
  872. break;
  873. case VID_API_EVENT_REQ_FRAME_BUFF:
  874. vpu_malone_unpack_req_frame(pkt, data);
  875. break;
  876. case VID_API_EVENT_REL_FRAME_BUFF:
  877. vpu_malone_unpack_rel_frame(pkt, data);
  878. break;
  879. case VID_API_EVENT_FRAME_BUFF_RDY:
  880. vpu_malone_unpack_buff_rdy(pkt, data);
  881. break;
  882. }
  883. return 0;
  884. }
  885. static const struct malone_padding_scode padding_scodes[] = {
  886. {SCODE_PADDING_EOS, V4L2_PIX_FMT_H264, {0x0B010000, 0}},
  887. {SCODE_PADDING_EOS, V4L2_PIX_FMT_H264_MVC, {0x0B010000, 0}},
  888. {SCODE_PADDING_EOS, V4L2_PIX_FMT_HEVC, {0x4A010000, 0x20}},
  889. {SCODE_PADDING_EOS, V4L2_PIX_FMT_VC1_ANNEX_G, {0x0a010000, 0x0}},
  890. {SCODE_PADDING_EOS, V4L2_PIX_FMT_VC1_ANNEX_L, {0x0a010000, 0x0}},
  891. {SCODE_PADDING_EOS, V4L2_PIX_FMT_MPEG2, {0xCC010000, 0x0}},
  892. {SCODE_PADDING_EOS, V4L2_PIX_FMT_MPEG4, {0xb1010000, 0x0}},
  893. {SCODE_PADDING_EOS, V4L2_PIX_FMT_XVID, {0xb1010000, 0x0}},
  894. {SCODE_PADDING_EOS, V4L2_PIX_FMT_H263, {0xb1010000, 0x0}},
  895. {SCODE_PADDING_EOS, V4L2_PIX_FMT_VP8, {0x34010000, 0x0}},
  896. {SCODE_PADDING_EOS, V4L2_PIX_FMT_SPK, {0x34010000, 0x0}},
  897. {SCODE_PADDING_EOS, V4L2_PIX_FMT_RV30, {0x34010000, 0x0}},
  898. {SCODE_PADDING_EOS, V4L2_PIX_FMT_RV40, {0x34010000, 0x0}},
  899. {SCODE_PADDING_EOS, V4L2_PIX_FMT_JPEG, {0xefff0000, 0x0}},
  900. {SCODE_PADDING_ABORT, V4L2_PIX_FMT_H264, {0x0B010000, 0}},
  901. {SCODE_PADDING_ABORT, V4L2_PIX_FMT_H264_MVC, {0x0B010000, 0}},
  902. {SCODE_PADDING_ABORT, V4L2_PIX_FMT_HEVC, {0x4A010000, 0x20}},
  903. {SCODE_PADDING_ABORT, V4L2_PIX_FMT_VC1_ANNEX_G, {0x0a010000, 0x0}},
  904. {SCODE_PADDING_ABORT, V4L2_PIX_FMT_VC1_ANNEX_L, {0x0a010000, 0x0}},
  905. {SCODE_PADDING_ABORT, V4L2_PIX_FMT_MPEG2, {0xb7010000, 0x0}},
  906. {SCODE_PADDING_ABORT, V4L2_PIX_FMT_MPEG4, {0xb1010000, 0x0}},
  907. {SCODE_PADDING_ABORT, V4L2_PIX_FMT_XVID, {0xb1010000, 0x0}},
  908. {SCODE_PADDING_ABORT, V4L2_PIX_FMT_H263, {0xb1010000, 0x0}},
  909. {SCODE_PADDING_ABORT, V4L2_PIX_FMT_VP8, {0x34010000, 0x0}},
  910. {SCODE_PADDING_ABORT, V4L2_PIX_FMT_SPK, {0x34010000, 0x0}},
  911. {SCODE_PADDING_ABORT, V4L2_PIX_FMT_RV30, {0x34010000, 0x0}},
  912. {SCODE_PADDING_ABORT, V4L2_PIX_FMT_RV40, {0x34010000, 0x0}},
  913. {SCODE_PADDING_EOS, V4L2_PIX_FMT_JPEG, {0x0, 0x0}},
  914. {SCODE_PADDING_BUFFLUSH, V4L2_PIX_FMT_H264, {0x15010000, 0x0}},
  915. {SCODE_PADDING_BUFFLUSH, V4L2_PIX_FMT_H264_MVC, {0x15010000, 0x0}},
  916. {SCODE_PADDING_BUFFLUSH, V4L2_PIX_FMT_HEVC, {0x3e010000, 0x20}},
  917. };
  918. static const struct malone_padding_scode padding_scode_dft = {0x0, 0x0};
  919. static const struct malone_padding_scode *get_padding_scode(u32 type, u32 fmt)
  920. {
  921. const struct malone_padding_scode *s;
  922. int i;
  923. for (i = 0; i < ARRAY_SIZE(padding_scodes); i++) {
  924. s = &padding_scodes[i];
  925. if (s->scode_type == type && s->pixelformat == fmt)
  926. return s;
  927. }
  928. if (type != SCODE_PADDING_BUFFLUSH)
  929. return &padding_scode_dft;
  930. return NULL;
  931. }
  932. static int vpu_malone_add_padding_scode(struct vpu_buffer *stream_buffer,
  933. struct vpu_malone_str_buffer __iomem *str_buf,
  934. u32 pixelformat, u32 scode_type)
  935. {
  936. u32 wptr;
  937. int size;
  938. int total_size = 0;
  939. const struct malone_padding_scode *ps;
  940. const u32 padding_size = 4096;
  941. int ret;
  942. ps = get_padding_scode(scode_type, pixelformat);
  943. if (!ps) {
  944. if (scode_type == SCODE_PADDING_BUFFLUSH)
  945. return 0;
  946. return -EINVAL;
  947. }
  948. wptr = readl(&str_buf->wptr);
  949. if (wptr < stream_buffer->phys || wptr > stream_buffer->phys + stream_buffer->length)
  950. return -EINVAL;
  951. if (wptr == stream_buffer->phys + stream_buffer->length)
  952. wptr = stream_buffer->phys;
  953. size = ALIGN(wptr, 4) - wptr;
  954. if (size)
  955. vpu_helper_memset_stream_buffer(stream_buffer, &wptr, 0, size);
  956. total_size += size;
  957. size = sizeof(ps->data);
  958. ret = vpu_helper_copy_to_stream_buffer(stream_buffer, &wptr, size, (void *)ps->data);
  959. if (ret < 0)
  960. return -EINVAL;
  961. total_size += size;
  962. size = padding_size - sizeof(ps->data);
  963. vpu_helper_memset_stream_buffer(stream_buffer, &wptr, 0, size);
  964. total_size += size;
  965. vpu_malone_update_wptr(str_buf, wptr);
  966. return total_size;
  967. }
  968. int vpu_malone_add_scode(struct vpu_shared_addr *shared,
  969. u32 instance,
  970. struct vpu_buffer *stream_buffer,
  971. u32 pixelformat,
  972. u32 scode_type)
  973. {
  974. struct vpu_dec_ctrl *hc = shared->priv;
  975. struct vpu_malone_str_buffer __iomem *str_buf = hc->str_buf[instance];
  976. int ret = -EINVAL;
  977. switch (scode_type) {
  978. case SCODE_PADDING_EOS:
  979. case SCODE_PADDING_ABORT:
  980. case SCODE_PADDING_BUFFLUSH:
  981. ret = vpu_malone_add_padding_scode(stream_buffer, str_buf, pixelformat, scode_type);
  982. break;
  983. default:
  984. break;
  985. }
  986. return ret;
  987. }
  988. #define MALONE_PAYLOAD_HEADER_SIZE 16
  989. #define MALONE_CODEC_VERSION_ID 0x1
  990. #define MALONE_CODEC_ID_VC1_SIMPLE 0x10
  991. #define MALONE_CODEC_ID_VC1_MAIN 0x11
  992. #define MALONE_CODEC_ID_ARV8 0x28
  993. #define MALONE_CODEC_ID_ARV9 0x29
  994. #define MALONE_CODEC_ID_VP6 0x36
  995. #define MALONE_CODEC_ID_VP8 0x36
  996. #define MALONE_CODEC_ID_DIVX3 0x38
  997. #define MALONE_CODEC_ID_SPK 0x39
  998. #define MALONE_VP8_IVF_SEQ_HEADER_LEN 32
  999. #define MALONE_VP8_IVF_FRAME_HEADER_LEN 8
  1000. #define MALONE_VC1_RCV_CODEC_V1_VERSION 0x85
  1001. #define MALONE_VC1_RCV_CODEC_V2_VERSION 0xC5
  1002. #define MALONE_VC1_RCV_NUM_FRAMES 0xFF
  1003. #define MALONE_VC1_RCV_SEQ_EXT_DATA_SIZE 4
  1004. #define MALONE_VC1_RCV_SEQ_HEADER_LEN 20
  1005. #define MALONE_VC1_RCV_PIC_HEADER_LEN 4
  1006. #define MALONE_VC1_NAL_HEADER_LEN 4
  1007. #define MALONE_VC1_CONTAIN_NAL(data) (((data) & 0x00FFFFFF) == 0x00010000)
  1008. static void set_payload_hdr(u8 *dst, u32 scd_type, u32 codec_id,
  1009. u32 buffer_size, u32 width, u32 height)
  1010. {
  1011. unsigned int payload_size;
  1012. /* payload_size = buffer_size + itself_size(16) - start_code(4) */
  1013. payload_size = buffer_size + 12;
  1014. dst[0] = 0x00;
  1015. dst[1] = 0x00;
  1016. dst[2] = 0x01;
  1017. dst[3] = scd_type;
  1018. /* length */
  1019. dst[4] = ((payload_size >> 16) & 0xff);
  1020. dst[5] = ((payload_size >> 8) & 0xff);
  1021. dst[6] = 0x4e;
  1022. dst[7] = ((payload_size >> 0) & 0xff);
  1023. /* Codec ID and Version */
  1024. dst[8] = codec_id;
  1025. dst[9] = MALONE_CODEC_VERSION_ID;
  1026. /* width */
  1027. dst[10] = ((width >> 8) & 0xff);
  1028. dst[11] = ((width >> 0) & 0xff);
  1029. dst[12] = 0x58;
  1030. /* height */
  1031. dst[13] = ((height >> 8) & 0xff);
  1032. dst[14] = ((height >> 0) & 0xff);
  1033. dst[15] = 0x50;
  1034. }
  1035. static void set_vp8_ivf_seqhdr(u8 *dst, u32 width, u32 height)
  1036. {
  1037. /* 0-3byte signature "DKIF" */
  1038. dst[0] = 0x44;
  1039. dst[1] = 0x4b;
  1040. dst[2] = 0x49;
  1041. dst[3] = 0x46;
  1042. /* 4-5byte version: should be 0*/
  1043. dst[4] = 0x00;
  1044. dst[5] = 0x00;
  1045. /* 6-7 length of Header */
  1046. dst[6] = MALONE_VP8_IVF_SEQ_HEADER_LEN;
  1047. dst[7] = MALONE_VP8_IVF_SEQ_HEADER_LEN >> 8;
  1048. /* 8-11 VP8 fourcc */
  1049. dst[8] = 0x56;
  1050. dst[9] = 0x50;
  1051. dst[10] = 0x38;
  1052. dst[11] = 0x30;
  1053. /* 12-13 width in pixels */
  1054. dst[12] = width;
  1055. dst[13] = width >> 8;
  1056. /* 14-15 height in pixels */
  1057. dst[14] = height;
  1058. dst[15] = height >> 8;
  1059. /* 16-19 frame rate */
  1060. dst[16] = 0xe8;
  1061. dst[17] = 0x03;
  1062. dst[18] = 0x00;
  1063. dst[19] = 0x00;
  1064. /* 20-23 time scale */
  1065. dst[20] = 0x01;
  1066. dst[21] = 0x00;
  1067. dst[22] = 0x00;
  1068. dst[23] = 0x00;
  1069. /* 24-27 number frames */
  1070. dst[24] = 0xdf;
  1071. dst[25] = 0xf9;
  1072. dst[26] = 0x09;
  1073. dst[27] = 0x00;
  1074. /* 28-31 reserved */
  1075. }
  1076. static void set_vp8_ivf_pichdr(u8 *dst, u32 frame_size)
  1077. {
  1078. /*
  1079. * firmware just parse 64-bit timestamp(8 bytes).
  1080. * As not transfer timestamp to firmware, use default value(ZERO).
  1081. * No need to do anything here
  1082. */
  1083. }
  1084. static void set_vc1_rcv_seqhdr(u8 *dst, u8 *src, u32 width, u32 height)
  1085. {
  1086. u32 frames = MALONE_VC1_RCV_NUM_FRAMES;
  1087. u32 ext_data_size = MALONE_VC1_RCV_SEQ_EXT_DATA_SIZE;
  1088. /* 0-2 Number of frames, used default value 0xFF */
  1089. dst[0] = frames;
  1090. dst[1] = frames >> 8;
  1091. dst[2] = frames >> 16;
  1092. /* 3 RCV version, used V1 */
  1093. dst[3] = MALONE_VC1_RCV_CODEC_V1_VERSION;
  1094. /* 4-7 extension data size */
  1095. dst[4] = ext_data_size;
  1096. dst[5] = ext_data_size >> 8;
  1097. dst[6] = ext_data_size >> 16;
  1098. dst[7] = ext_data_size >> 24;
  1099. /* 8-11 extension data */
  1100. dst[8] = src[0];
  1101. dst[9] = src[1];
  1102. dst[10] = src[2];
  1103. dst[11] = src[3];
  1104. /* height */
  1105. dst[12] = height;
  1106. dst[13] = (height >> 8) & 0xff;
  1107. dst[14] = (height >> 16) & 0xff;
  1108. dst[15] = (height >> 24) & 0xff;
  1109. /* width */
  1110. dst[16] = width;
  1111. dst[17] = (width >> 8) & 0xff;
  1112. dst[18] = (width >> 16) & 0xff;
  1113. dst[19] = (width >> 24) & 0xff;
  1114. }
  1115. static void set_vc1_rcv_pichdr(u8 *dst, u32 buffer_size)
  1116. {
  1117. dst[0] = buffer_size;
  1118. dst[1] = buffer_size >> 8;
  1119. dst[2] = buffer_size >> 16;
  1120. dst[3] = buffer_size >> 24;
  1121. }
  1122. static void create_vc1_nal_pichdr(u8 *dst)
  1123. {
  1124. /* need insert nal header: special ID */
  1125. dst[0] = 0x0;
  1126. dst[1] = 0x0;
  1127. dst[2] = 0x01;
  1128. dst[3] = 0x0D;
  1129. }
  1130. static int vpu_malone_insert_scode_seq(struct malone_scode_t *scode, u32 codec_id, u32 ext_size)
  1131. {
  1132. u8 hdr[MALONE_PAYLOAD_HEADER_SIZE];
  1133. int ret;
  1134. set_payload_hdr(hdr,
  1135. SCODE_SEQUENCE,
  1136. codec_id,
  1137. ext_size,
  1138. scode->inst->out_format.width,
  1139. scode->inst->out_format.height);
  1140. ret = vpu_helper_copy_to_stream_buffer(&scode->inst->stream_buffer,
  1141. &scode->wptr,
  1142. sizeof(hdr),
  1143. hdr);
  1144. if (ret < 0)
  1145. return ret;
  1146. return sizeof(hdr);
  1147. }
  1148. static int vpu_malone_insert_scode_pic(struct malone_scode_t *scode, u32 codec_id, u32 ext_size)
  1149. {
  1150. u8 hdr[MALONE_PAYLOAD_HEADER_SIZE];
  1151. int ret;
  1152. set_payload_hdr(hdr,
  1153. SCODE_PICTURE,
  1154. codec_id,
  1155. ext_size + vb2_get_plane_payload(scode->vb, 0),
  1156. scode->inst->out_format.width,
  1157. scode->inst->out_format.height);
  1158. ret = vpu_helper_copy_to_stream_buffer(&scode->inst->stream_buffer,
  1159. &scode->wptr,
  1160. sizeof(hdr),
  1161. hdr);
  1162. if (ret < 0)
  1163. return ret;
  1164. return sizeof(hdr);
  1165. }
  1166. static int vpu_malone_insert_scode_vc1_g_seq(struct malone_scode_t *scode)
  1167. {
  1168. if (!scode->inst->total_input_count)
  1169. return 0;
  1170. return 0;
  1171. }
  1172. static int vpu_malone_insert_scode_vc1_g_pic(struct malone_scode_t *scode)
  1173. {
  1174. u8 nal_hdr[MALONE_VC1_NAL_HEADER_LEN];
  1175. u32 *data = NULL;
  1176. int ret;
  1177. data = vb2_plane_vaddr(scode->vb, 0);
  1178. if (scode->inst->total_input_count == 0)
  1179. return 0;
  1180. if (MALONE_VC1_CONTAIN_NAL(*data))
  1181. return 0;
  1182. create_vc1_nal_pichdr(nal_hdr);
  1183. ret = vpu_helper_copy_to_stream_buffer(&scode->inst->stream_buffer,
  1184. &scode->wptr,
  1185. sizeof(nal_hdr),
  1186. nal_hdr);
  1187. if (ret < 0)
  1188. return ret;
  1189. return sizeof(nal_hdr);
  1190. }
  1191. static int vpu_malone_insert_scode_vc1_l_seq(struct malone_scode_t *scode)
  1192. {
  1193. int ret;
  1194. int size = 0;
  1195. u8 rcv_seqhdr[MALONE_VC1_RCV_SEQ_HEADER_LEN];
  1196. if (scode->inst->total_input_count)
  1197. return 0;
  1198. scode->need_data = 0;
  1199. ret = vpu_malone_insert_scode_seq(scode, MALONE_CODEC_ID_VC1_SIMPLE, sizeof(rcv_seqhdr));
  1200. if (ret < 0)
  1201. return ret;
  1202. size = ret;
  1203. set_vc1_rcv_seqhdr(rcv_seqhdr,
  1204. vb2_plane_vaddr(scode->vb, 0),
  1205. scode->inst->out_format.width,
  1206. scode->inst->out_format.height);
  1207. ret = vpu_helper_copy_to_stream_buffer(&scode->inst->stream_buffer,
  1208. &scode->wptr,
  1209. sizeof(rcv_seqhdr),
  1210. rcv_seqhdr);
  1211. if (ret < 0)
  1212. return ret;
  1213. size += sizeof(rcv_seqhdr);
  1214. return size;
  1215. }
  1216. static int vpu_malone_insert_scode_vc1_l_pic(struct malone_scode_t *scode)
  1217. {
  1218. int ret;
  1219. int size = 0;
  1220. u8 rcv_pichdr[MALONE_VC1_RCV_PIC_HEADER_LEN];
  1221. ret = vpu_malone_insert_scode_pic(scode, MALONE_CODEC_ID_VC1_SIMPLE,
  1222. sizeof(rcv_pichdr));
  1223. if (ret < 0)
  1224. return ret;
  1225. size = ret;
  1226. set_vc1_rcv_pichdr(rcv_pichdr, vb2_get_plane_payload(scode->vb, 0));
  1227. ret = vpu_helper_copy_to_stream_buffer(&scode->inst->stream_buffer,
  1228. &scode->wptr,
  1229. sizeof(rcv_pichdr),
  1230. rcv_pichdr);
  1231. if (ret < 0)
  1232. return ret;
  1233. size += sizeof(rcv_pichdr);
  1234. return size;
  1235. }
  1236. static int vpu_malone_insert_scode_vp8_seq(struct malone_scode_t *scode)
  1237. {
  1238. int ret;
  1239. int size = 0;
  1240. u8 ivf_hdr[MALONE_VP8_IVF_SEQ_HEADER_LEN];
  1241. ret = vpu_malone_insert_scode_seq(scode, MALONE_CODEC_ID_VP8, sizeof(ivf_hdr));
  1242. if (ret < 0)
  1243. return ret;
  1244. size = ret;
  1245. set_vp8_ivf_seqhdr(ivf_hdr,
  1246. scode->inst->out_format.width,
  1247. scode->inst->out_format.height);
  1248. ret = vpu_helper_copy_to_stream_buffer(&scode->inst->stream_buffer,
  1249. &scode->wptr,
  1250. sizeof(ivf_hdr),
  1251. ivf_hdr);
  1252. if (ret < 0)
  1253. return ret;
  1254. size += sizeof(ivf_hdr);
  1255. return size;
  1256. }
  1257. static int vpu_malone_insert_scode_vp8_pic(struct malone_scode_t *scode)
  1258. {
  1259. int ret;
  1260. int size = 0;
  1261. u8 ivf_hdr[MALONE_VP8_IVF_FRAME_HEADER_LEN] = {0};
  1262. ret = vpu_malone_insert_scode_pic(scode, MALONE_CODEC_ID_VP8, sizeof(ivf_hdr));
  1263. if (ret < 0)
  1264. return ret;
  1265. size = ret;
  1266. set_vp8_ivf_pichdr(ivf_hdr, vb2_get_plane_payload(scode->vb, 0));
  1267. ret = vpu_helper_copy_to_stream_buffer(&scode->inst->stream_buffer,
  1268. &scode->wptr,
  1269. sizeof(ivf_hdr),
  1270. ivf_hdr);
  1271. if (ret < 0)
  1272. return ret;
  1273. size += sizeof(ivf_hdr);
  1274. return size;
  1275. }
  1276. static int vpu_malone_insert_scode_spk_seq(struct malone_scode_t *scode)
  1277. {
  1278. return vpu_malone_insert_scode_seq(scode, MALONE_CODEC_ID_SPK, 0);
  1279. }
  1280. static int vpu_malone_insert_scode_spk_pic(struct malone_scode_t *scode)
  1281. {
  1282. return vpu_malone_insert_scode_pic(scode, MALONE_CODEC_ID_SPK, 0);
  1283. }
  1284. static const struct malone_scode_handler scode_handlers[] = {
  1285. {
  1286. /* fix me, need to swap return operation after gstreamer swap */
  1287. .pixelformat = V4L2_PIX_FMT_VC1_ANNEX_L,
  1288. .insert_scode_seq = vpu_malone_insert_scode_vc1_l_seq,
  1289. .insert_scode_pic = vpu_malone_insert_scode_vc1_l_pic,
  1290. },
  1291. {
  1292. .pixelformat = V4L2_PIX_FMT_VC1_ANNEX_G,
  1293. .insert_scode_seq = vpu_malone_insert_scode_vc1_g_seq,
  1294. .insert_scode_pic = vpu_malone_insert_scode_vc1_g_pic,
  1295. },
  1296. {
  1297. .pixelformat = V4L2_PIX_FMT_VP8,
  1298. .insert_scode_seq = vpu_malone_insert_scode_vp8_seq,
  1299. .insert_scode_pic = vpu_malone_insert_scode_vp8_pic,
  1300. },
  1301. {
  1302. .pixelformat = V4L2_PIX_FMT_SPK,
  1303. .insert_scode_seq = vpu_malone_insert_scode_spk_seq,
  1304. .insert_scode_pic = vpu_malone_insert_scode_spk_pic,
  1305. },
  1306. };
  1307. static const struct malone_scode_handler *get_scode_handler(u32 pixelformat)
  1308. {
  1309. int i;
  1310. for (i = 0; i < ARRAY_SIZE(scode_handlers); i++) {
  1311. if (scode_handlers[i].pixelformat == pixelformat)
  1312. return &scode_handlers[i];
  1313. }
  1314. return NULL;
  1315. }
  1316. static int vpu_malone_insert_scode(struct malone_scode_t *scode, u32 type)
  1317. {
  1318. const struct malone_scode_handler *handler;
  1319. int ret = 0;
  1320. if (!scode || !scode->inst || !scode->vb)
  1321. return 0;
  1322. scode->need_data = 1;
  1323. handler = get_scode_handler(scode->inst->out_format.pixfmt);
  1324. if (!handler)
  1325. return 0;
  1326. switch (type) {
  1327. case SCODE_SEQUENCE:
  1328. if (handler->insert_scode_seq)
  1329. ret = handler->insert_scode_seq(scode);
  1330. break;
  1331. case SCODE_PICTURE:
  1332. if (handler->insert_scode_pic)
  1333. ret = handler->insert_scode_pic(scode);
  1334. break;
  1335. default:
  1336. break;
  1337. }
  1338. return ret;
  1339. }
  1340. static int vpu_malone_input_frame_data(struct vpu_malone_str_buffer __iomem *str_buf,
  1341. struct vpu_inst *inst, struct vb2_buffer *vb,
  1342. u32 disp_imm)
  1343. {
  1344. struct malone_scode_t scode;
  1345. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1346. u32 wptr = readl(&str_buf->wptr);
  1347. int size = 0;
  1348. int ret = 0;
  1349. /*add scode: SCODE_SEQUENCE, SCODE_PICTURE, SCODE_SLICE*/
  1350. scode.inst = inst;
  1351. scode.vb = vb;
  1352. scode.wptr = wptr;
  1353. scode.need_data = 1;
  1354. if (vbuf->sequence == 0)
  1355. ret = vpu_malone_insert_scode(&scode, SCODE_SEQUENCE);
  1356. if (ret < 0)
  1357. return -ENOMEM;
  1358. size += ret;
  1359. wptr = scode.wptr;
  1360. if (!scode.need_data) {
  1361. vpu_malone_update_wptr(str_buf, wptr);
  1362. return size;
  1363. }
  1364. ret = vpu_malone_insert_scode(&scode, SCODE_PICTURE);
  1365. if (ret < 0)
  1366. return -ENOMEM;
  1367. size += ret;
  1368. wptr = scode.wptr;
  1369. ret = vpu_helper_copy_to_stream_buffer(&inst->stream_buffer,
  1370. &wptr,
  1371. vb2_get_plane_payload(vb, 0),
  1372. vb2_plane_vaddr(vb, 0));
  1373. if (ret < 0)
  1374. return -ENOMEM;
  1375. size += vb2_get_plane_payload(vb, 0);
  1376. vpu_malone_update_wptr(str_buf, wptr);
  1377. /*
  1378. * Enable the low latency flush mode if display delay is set to 0
  1379. * or the low latency frame flush mode if it is set to 1.
  1380. * The low latency flush mode requires some padding data to be appended to each frame,
  1381. * but there must not be any padding data between the sequence header and the frame.
  1382. * This module is currently only supported for the H264 and HEVC formats,
  1383. * for other formats, vpu_malone_add_scode() will return 0.
  1384. */
  1385. if (disp_imm || low_latency) {
  1386. ret = vpu_malone_add_scode(inst->core->iface,
  1387. inst->id,
  1388. &inst->stream_buffer,
  1389. inst->out_format.pixfmt,
  1390. SCODE_PADDING_BUFFLUSH);
  1391. if (ret < 0)
  1392. return ret;
  1393. size += ret;
  1394. }
  1395. return size;
  1396. }
  1397. static int vpu_malone_input_stream_data(struct vpu_malone_str_buffer __iomem *str_buf,
  1398. struct vpu_inst *inst, struct vb2_buffer *vb)
  1399. {
  1400. u32 wptr = readl(&str_buf->wptr);
  1401. int ret = 0;
  1402. ret = vpu_helper_copy_to_stream_buffer(&inst->stream_buffer,
  1403. &wptr,
  1404. vb2_get_plane_payload(vb, 0),
  1405. vb2_plane_vaddr(vb, 0));
  1406. if (ret < 0)
  1407. return -ENOMEM;
  1408. vpu_malone_update_wptr(str_buf, wptr);
  1409. return ret;
  1410. }
  1411. static int vpu_malone_input_ts(struct vpu_inst *inst, s64 timestamp, u32 size)
  1412. {
  1413. struct vpu_ts_info info;
  1414. memset(&info, 0, sizeof(info));
  1415. info.timestamp = timestamp;
  1416. info.size = size;
  1417. return vpu_session_fill_timestamp(inst, &info);
  1418. }
  1419. int vpu_malone_input_frame(struct vpu_shared_addr *shared,
  1420. struct vpu_inst *inst, struct vb2_buffer *vb)
  1421. {
  1422. struct vpu_dec_ctrl *hc = shared->priv;
  1423. struct vpu_malone_str_buffer __iomem *str_buf = hc->str_buf[inst->id];
  1424. u32 disp_imm = hc->codec_param[inst->id].disp_imm;
  1425. u32 size;
  1426. int ret;
  1427. if (vpu_malone_is_non_frame_mode(shared, inst->id))
  1428. ret = vpu_malone_input_stream_data(str_buf, inst, vb);
  1429. else
  1430. ret = vpu_malone_input_frame_data(str_buf, inst, vb, disp_imm);
  1431. if (ret < 0)
  1432. return ret;
  1433. size = ret;
  1434. if (inst->extra_size) {
  1435. size += inst->extra_size;
  1436. inst->extra_size = 0;
  1437. }
  1438. ret = vpu_malone_input_ts(inst, vb->timestamp, size);
  1439. if (ret)
  1440. return ret;
  1441. return 0;
  1442. }
  1443. static bool vpu_malone_check_ready(struct vpu_shared_addr *shared, u32 instance)
  1444. {
  1445. struct malone_iface *iface = shared->iface;
  1446. struct vpu_rpc_buffer_desc *desc = &iface->api_cmd_buffer_desc[instance];
  1447. u32 size = desc->end - desc->start;
  1448. u32 rptr = desc->rptr;
  1449. u32 wptr = desc->wptr;
  1450. u32 used;
  1451. if (!size)
  1452. return true;
  1453. used = (wptr + size - rptr) % size;
  1454. if (used < (size / 2))
  1455. return true;
  1456. return false;
  1457. }
  1458. bool vpu_malone_is_ready(struct vpu_shared_addr *shared, u32 instance)
  1459. {
  1460. u32 cnt = 0;
  1461. while (!vpu_malone_check_ready(shared, instance)) {
  1462. if (cnt > 30)
  1463. return false;
  1464. mdelay(1);
  1465. cnt++;
  1466. }
  1467. return true;
  1468. }
  1469. int vpu_malone_pre_cmd(struct vpu_shared_addr *shared, u32 instance)
  1470. {
  1471. if (!vpu_malone_is_ready(shared, instance))
  1472. return -EINVAL;
  1473. return 0;
  1474. }
  1475. int vpu_malone_post_cmd(struct vpu_shared_addr *shared, u32 instance)
  1476. {
  1477. struct malone_iface *iface = shared->iface;
  1478. struct vpu_rpc_buffer_desc *desc = &iface->api_cmd_buffer_desc[instance];
  1479. desc->wptr++;
  1480. if (desc->wptr == desc->end)
  1481. desc->wptr = desc->start;
  1482. return 0;
  1483. }
  1484. int vpu_malone_init_instance(struct vpu_shared_addr *shared, u32 instance)
  1485. {
  1486. struct malone_iface *iface = shared->iface;
  1487. struct vpu_rpc_buffer_desc *desc = &iface->api_cmd_buffer_desc[instance];
  1488. desc->wptr = desc->rptr;
  1489. if (desc->wptr == desc->end)
  1490. desc->wptr = desc->start;
  1491. return 0;
  1492. }
  1493. u32 vpu_malone_get_max_instance_count(struct vpu_shared_addr *shared)
  1494. {
  1495. struct malone_iface *iface = shared->iface;
  1496. return iface->max_streams;
  1497. }