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