coda.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Coda multi-standard codec IP
  4. *
  5. * Copyright (C) 2012 Vista Silicon S.L.
  6. * Javier Martin, <[email protected]>
  7. * Xavier Duret
  8. * Copyright (C) 2012-2014 Philipp Zabel, Pengutronix
  9. */
  10. #ifndef __CODA_H__
  11. #define __CODA_H__
  12. #include <linux/debugfs.h>
  13. #include <linux/idr.h>
  14. #include <linux/irqreturn.h>
  15. #include <linux/mutex.h>
  16. #include <linux/kfifo.h>
  17. #include <linux/videodev2.h>
  18. #include <linux/ratelimit.h>
  19. #include <media/v4l2-ctrls.h>
  20. #include <media/v4l2-device.h>
  21. #include <media/v4l2-fh.h>
  22. #include <media/videobuf2-v4l2.h>
  23. #include "coda_regs.h"
  24. #define CODA_MAX_FRAMEBUFFERS 19
  25. #define FMO_SLICE_SAVE_BUF_SIZE (32)
  26. /*
  27. * This control allows applications to read the per-stream
  28. * (i.e. per-context) Macroblocks Error Count. This value
  29. * is CODA specific.
  30. */
  31. #define V4L2_CID_CODA_MB_ERR_CNT (V4L2_CID_USER_CODA_BASE + 0)
  32. enum {
  33. V4L2_M2M_SRC = 0,
  34. V4L2_M2M_DST = 1,
  35. };
  36. enum coda_inst_type {
  37. CODA_INST_ENCODER,
  38. CODA_INST_DECODER,
  39. };
  40. enum coda_product {
  41. CODA_DX6 = 0xf001,
  42. CODA_HX4 = 0xf00a,
  43. CODA_7541 = 0xf012,
  44. CODA_960 = 0xf020,
  45. };
  46. struct coda_video_device;
  47. struct coda_devtype {
  48. char *firmware[3];
  49. enum coda_product product;
  50. const struct coda_codec *codecs;
  51. unsigned int num_codecs;
  52. const struct coda_video_device **vdevs;
  53. unsigned int num_vdevs;
  54. size_t workbuf_size;
  55. size_t tempbuf_size;
  56. size_t iram_size;
  57. };
  58. struct coda_aux_buf {
  59. void *vaddr;
  60. dma_addr_t paddr;
  61. u32 size;
  62. struct debugfs_blob_wrapper blob;
  63. struct dentry *dentry;
  64. };
  65. struct coda_dev {
  66. struct v4l2_device v4l2_dev;
  67. struct video_device vfd[6];
  68. struct device *dev;
  69. const struct coda_devtype *devtype;
  70. int firmware;
  71. struct vdoa_data *vdoa;
  72. void __iomem *regs_base;
  73. struct clk *clk_per;
  74. struct clk *clk_ahb;
  75. struct reset_control *rstc;
  76. struct coda_aux_buf codebuf;
  77. struct coda_aux_buf tempbuf;
  78. struct coda_aux_buf workbuf;
  79. struct gen_pool *iram_pool;
  80. struct coda_aux_buf iram;
  81. struct mutex dev_mutex;
  82. struct mutex coda_mutex;
  83. struct workqueue_struct *workqueue;
  84. struct v4l2_m2m_dev *m2m_dev;
  85. struct ida ida;
  86. struct dentry *debugfs_root;
  87. struct ratelimit_state mb_err_rs;
  88. };
  89. struct coda_codec {
  90. u32 mode;
  91. u32 src_fourcc;
  92. u32 dst_fourcc;
  93. u32 max_w;
  94. u32 max_h;
  95. };
  96. struct coda_huff_tab;
  97. struct coda_params {
  98. u8 rot_mode;
  99. u8 h264_intra_qp;
  100. u8 h264_inter_qp;
  101. u8 h264_min_qp;
  102. u8 h264_max_qp;
  103. u8 h264_disable_deblocking_filter_idc;
  104. s8 h264_slice_alpha_c0_offset_div2;
  105. s8 h264_slice_beta_offset_div2;
  106. bool h264_constrained_intra_pred_flag;
  107. s8 h264_chroma_qp_index_offset;
  108. u8 h264_profile_idc;
  109. u8 h264_level_idc;
  110. u8 mpeg2_profile_idc;
  111. u8 mpeg2_level_idc;
  112. u8 mpeg4_intra_qp;
  113. u8 mpeg4_inter_qp;
  114. u8 gop_size;
  115. int intra_refresh;
  116. enum v4l2_jpeg_chroma_subsampling jpeg_chroma_subsampling;
  117. u8 jpeg_quality;
  118. u8 jpeg_restart_interval;
  119. u8 *jpeg_qmat_tab[3];
  120. int jpeg_qmat_index[3];
  121. int jpeg_huff_dc_index[3];
  122. int jpeg_huff_ac_index[3];
  123. u32 *jpeg_huff_data;
  124. struct coda_huff_tab *jpeg_huff_tab;
  125. int codec_mode;
  126. int codec_mode_aux;
  127. enum v4l2_mpeg_video_multi_slice_mode slice_mode;
  128. u32 framerate;
  129. u16 bitrate;
  130. u16 vbv_delay;
  131. u32 vbv_size;
  132. u32 slice_max_bits;
  133. u32 slice_max_mb;
  134. bool force_ipicture;
  135. bool gop_size_changed;
  136. bool bitrate_changed;
  137. bool framerate_changed;
  138. bool h264_intra_qp_changed;
  139. bool intra_refresh_changed;
  140. bool slice_mode_changed;
  141. bool frame_rc_enable;
  142. bool mb_rc_enable;
  143. };
  144. struct coda_buffer_meta {
  145. struct list_head list;
  146. u32 sequence;
  147. struct v4l2_timecode timecode;
  148. u64 timestamp;
  149. unsigned int start;
  150. unsigned int end;
  151. bool last;
  152. };
  153. /* Per-queue, driver-specific private data */
  154. struct coda_q_data {
  155. unsigned int width;
  156. unsigned int height;
  157. unsigned int bytesperline;
  158. unsigned int sizeimage;
  159. unsigned int fourcc;
  160. struct v4l2_rect rect;
  161. };
  162. struct coda_iram_info {
  163. u32 axi_sram_use;
  164. phys_addr_t buf_bit_use;
  165. phys_addr_t buf_ip_ac_dc_use;
  166. phys_addr_t buf_dbk_y_use;
  167. phys_addr_t buf_dbk_c_use;
  168. phys_addr_t buf_ovl_use;
  169. phys_addr_t buf_btp_use;
  170. phys_addr_t search_ram_paddr;
  171. int search_ram_size;
  172. int remaining;
  173. phys_addr_t next_paddr;
  174. };
  175. #define GDI_LINEAR_FRAME_MAP 0
  176. #define GDI_TILED_FRAME_MB_RASTER_MAP 1
  177. struct coda_ctx;
  178. struct coda_context_ops {
  179. int (*queue_init)(void *priv, struct vb2_queue *src_vq,
  180. struct vb2_queue *dst_vq);
  181. int (*reqbufs)(struct coda_ctx *ctx, struct v4l2_requestbuffers *rb);
  182. int (*start_streaming)(struct coda_ctx *ctx);
  183. int (*prepare_run)(struct coda_ctx *ctx);
  184. void (*finish_run)(struct coda_ctx *ctx);
  185. void (*run_timeout)(struct coda_ctx *ctx);
  186. void (*seq_init_work)(struct work_struct *work);
  187. void (*seq_end_work)(struct work_struct *work);
  188. void (*release)(struct coda_ctx *ctx);
  189. };
  190. struct coda_internal_frame {
  191. struct coda_aux_buf buf;
  192. struct coda_buffer_meta meta;
  193. u32 type;
  194. u32 error;
  195. };
  196. struct coda_ctx {
  197. struct coda_dev *dev;
  198. struct mutex buffer_mutex;
  199. struct work_struct pic_run_work;
  200. struct work_struct seq_init_work;
  201. struct work_struct seq_end_work;
  202. struct completion completion;
  203. const struct coda_video_device *cvd;
  204. const struct coda_context_ops *ops;
  205. int aborting;
  206. int initialized;
  207. int streamon_out;
  208. int streamon_cap;
  209. u32 qsequence;
  210. u32 osequence;
  211. u32 sequence_offset;
  212. struct coda_q_data q_data[2];
  213. enum coda_inst_type inst_type;
  214. const struct coda_codec *codec;
  215. enum v4l2_colorspace colorspace;
  216. enum v4l2_xfer_func xfer_func;
  217. enum v4l2_ycbcr_encoding ycbcr_enc;
  218. enum v4l2_quantization quantization;
  219. struct coda_params params;
  220. struct v4l2_ctrl_handler ctrls;
  221. struct v4l2_ctrl *h264_profile_ctrl;
  222. struct v4l2_ctrl *h264_level_ctrl;
  223. struct v4l2_ctrl *mpeg2_profile_ctrl;
  224. struct v4l2_ctrl *mpeg2_level_ctrl;
  225. struct v4l2_ctrl *mpeg4_profile_ctrl;
  226. struct v4l2_ctrl *mpeg4_level_ctrl;
  227. struct v4l2_ctrl *mb_err_cnt_ctrl;
  228. struct v4l2_fh fh;
  229. int gopcounter;
  230. int runcounter;
  231. int jpeg_ecs_offset;
  232. char vpu_header[3][64];
  233. int vpu_header_size[3];
  234. struct kfifo bitstream_fifo;
  235. struct mutex bitstream_mutex;
  236. struct coda_aux_buf bitstream;
  237. bool hold;
  238. struct coda_aux_buf parabuf;
  239. struct coda_aux_buf psbuf;
  240. struct coda_aux_buf slicebuf;
  241. struct coda_internal_frame internal_frames[CODA_MAX_FRAMEBUFFERS];
  242. struct list_head buffer_meta_list;
  243. spinlock_t buffer_meta_lock;
  244. int num_metas;
  245. unsigned int first_frame_sequence;
  246. struct coda_aux_buf workbuf;
  247. int num_internal_frames;
  248. int idx;
  249. int reg_idx;
  250. struct coda_iram_info iram_info;
  251. int tiled_map_type;
  252. u32 bit_stream_param;
  253. u32 frm_dis_flg;
  254. u32 frame_mem_ctrl;
  255. u32 para_change;
  256. int display_idx;
  257. struct dentry *debugfs_entry;
  258. bool use_bit;
  259. bool use_vdoa;
  260. struct vdoa_ctx *vdoa;
  261. /*
  262. * wakeup mutex used to serialize encoder stop command and finish_run,
  263. * ensures that finish_run always either flags the last returned buffer
  264. * or wakes up the capture queue to signal EOS afterwards.
  265. */
  266. struct mutex wakeup_mutex;
  267. };
  268. extern int coda_debug;
  269. #define coda_dbg(level, ctx, fmt, arg...) \
  270. do { \
  271. if (coda_debug >= (level)) \
  272. v4l2_dbg((level), coda_debug, &(ctx)->dev->v4l2_dev, \
  273. "%u: " fmt, (ctx)->idx, ##arg); \
  274. } while (0)
  275. void coda_write(struct coda_dev *dev, u32 data, u32 reg);
  276. unsigned int coda_read(struct coda_dev *dev, u32 reg);
  277. void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
  278. struct vb2_v4l2_buffer *buf, unsigned int reg_y);
  279. int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
  280. size_t size, const char *name, struct dentry *parent);
  281. void coda_free_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf);
  282. int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
  283. struct vb2_queue *dst_vq);
  284. int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
  285. struct vb2_queue *dst_vq);
  286. int coda_hw_reset(struct coda_ctx *ctx);
  287. void coda_fill_bitstream(struct coda_ctx *ctx, struct list_head *buffer_list);
  288. void coda_set_gdi_regs(struct coda_ctx *ctx);
  289. static inline struct coda_q_data *get_q_data(struct coda_ctx *ctx,
  290. enum v4l2_buf_type type)
  291. {
  292. switch (type) {
  293. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  294. return &(ctx->q_data[V4L2_M2M_SRC]);
  295. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  296. return &(ctx->q_data[V4L2_M2M_DST]);
  297. default:
  298. return NULL;
  299. }
  300. }
  301. const char *coda_product_name(int product);
  302. int coda_check_firmware(struct coda_dev *dev);
  303. static inline unsigned int coda_get_bitstream_payload(struct coda_ctx *ctx)
  304. {
  305. return kfifo_len(&ctx->bitstream_fifo);
  306. }
  307. /*
  308. * The bitstream prefetcher needs to read at least 2 256 byte periods past
  309. * the desired bitstream position for all data to reach the decoder.
  310. */
  311. static inline bool coda_bitstream_can_fetch_past(struct coda_ctx *ctx,
  312. unsigned int pos)
  313. {
  314. return (int)(ctx->bitstream_fifo.kfifo.in - ALIGN(pos, 256)) > 512;
  315. }
  316. bool coda_bitstream_can_fetch_past(struct coda_ctx *ctx, unsigned int pos);
  317. int coda_bitstream_flush(struct coda_ctx *ctx);
  318. void coda_bit_stream_end_flag(struct coda_ctx *ctx);
  319. void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
  320. enum vb2_buffer_state state);
  321. int coda_h264_filler_nal(int size, char *p);
  322. int coda_h264_padding(int size, char *p);
  323. int coda_h264_profile(int profile_idc);
  324. int coda_h264_level(int level_idc);
  325. int coda_sps_parse_profile(struct coda_ctx *ctx, struct vb2_buffer *vb);
  326. int coda_h264_sps_fixup(struct coda_ctx *ctx, int width, int height, char *buf,
  327. int *size, int max_size);
  328. int coda_mpeg2_profile(int profile_idc);
  329. int coda_mpeg2_level(int level_idc);
  330. u32 coda_mpeg2_parse_headers(struct coda_ctx *ctx, u8 *buf, u32 size);
  331. int coda_mpeg4_profile(int profile_idc);
  332. int coda_mpeg4_level(int level_idc);
  333. u32 coda_mpeg4_parse_headers(struct coda_ctx *ctx, u8 *buf, u32 size);
  334. void coda_update_profile_level_ctrls(struct coda_ctx *ctx, u8 profile_idc,
  335. u8 level_idc);
  336. static inline int coda_jpeg_scale(int src, int dst)
  337. {
  338. return (dst <= src / 8) ? 3 :
  339. (dst <= src / 4) ? 2 :
  340. (dst <= src / 2) ? 1 : 0;
  341. }
  342. bool coda_jpeg_check_buffer(struct coda_ctx *ctx, struct vb2_buffer *vb);
  343. int coda_jpeg_decode_header(struct coda_ctx *ctx, struct vb2_buffer *vb);
  344. int coda_jpeg_write_tables(struct coda_ctx *ctx);
  345. void coda_set_jpeg_compression_quality(struct coda_ctx *ctx, int quality);
  346. extern const struct coda_context_ops coda_bit_encode_ops;
  347. extern const struct coda_context_ops coda_bit_decode_ops;
  348. extern const struct coda_context_ops coda9_jpeg_encode_ops;
  349. extern const struct coda_context_ops coda9_jpeg_decode_ops;
  350. irqreturn_t coda_irq_handler(int irq, void *data);
  351. irqreturn_t coda9_jpeg_irq_handler(int irq, void *data);
  352. #endif /* __CODA_H__ */